2013-12-10 222 views
-1

我想通过notifyDataSetChanged()这个方法更新我的列表视图,但listview没有得到更新,如果我按回去按钮,并转到上一个活动,然后再次,如果我进来活动,然后它正在更新,我不知道为什么。我尝试了所有可能的解决方案,但没有得到适当的解请帮助下面是我的代码。Listview没有得到更新notifyDataSetChanged方法

这里是我试图链接

ListView not getting updated on calling notifyDataSetChanged()

notifyDataSetChanged() not working

notifyDataSetChanged Android ListView

notifyDataSetChanged not updating ListView

The event of notifyDataSetChanged()

public class Assignment extends Activity { 

ListView mListView; 
ImageView imageViewCrtAsnm; 
String[] stg1; 
List<String[]> names2 = null; 
DataManipulator dataManipulator; 
ArrayAdapter<String> adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.assignment); 

    imageViewCrtAsnm = (ImageView) findViewById(R.id.createassignment); 
    mListView = (ListView) findViewById(R.id.displaydata); 

    imageViewCrtAsnm.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent intent = new Intent(Assignment.this, 
        Assignment_Create.class); 
      startActivity(intent); 
     } 
    }); 

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View item, 
       final int position, long id) { 

      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        Assignment.this); 
      alertDialogBuilder.setTitle("Delete Data"); 
      alertDialogBuilder 
        .setMessage("Click yes to Delete Record!") 
        .setCancelable(false) 
        .setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int id) { 
            String[] delete = names2.get(position); 
            String idString = delete[0]; 
            long idLong = Long.valueOf(idString); 
            Log.d("Deleting...", idLong + ""); 
            dataManipulator.delete(idLong); 
            names2.remove(position); 

            stg1 = new String[names2.size()]; 
            int x = 0; 
            String stg; 

            for (String[] name : names2) { 
             stg = "Title : " + name[1] + "\n" 
               + "Descr : " + name[2] 
               + "\n" + "Day's Left : " 
               + name[3]; 
             stg1[x] = stg; 
             x++; 
            } 
            adapter.notifyDataSetChanged(); 
           } 
          }) 
        .setNegativeButton("No", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int id) { 
            // if this button is clicked, just close 
            // the dialog box and do nothing 
            dialog.cancel(); 
           } 
          }); 
      AlertDialog alertDialog = alertDialogBuilder.create(); 
      alertDialog.show(); 
     } 
    }); 

    dataManipulator = new DataManipulator(this); 
    names2 = dataManipulator.selectAll(); 

    stg1 = new String[names2.size()]; 
    int x = 0; 
    String stg; 

    for (String[] name : names2) { 
     stg = "Title : " + name[1] + "\n" + "Descr : " + name[2] + "\n" 
       + "Day's Left : " + name[3]; 
     stg1[x] = stg; 
     x++; 
    } 

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, stg1); 
    mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.check, 
      stg1)); 
    mListView.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
} 
} 
+0

如果您尝试了所有可能的解决方案,那么就没有解决方案。但是,严肃地说,你至少应该指出你尝试了什么,所以其他人不会向你建议这些解决方案。 – Tafari

+0

@Tafari,我已编辑我的问题,我已经尝试过的链接,但仍然有其他链接,但我不能更新所有 – InnocentKiller

+0

无后顾之忧,现在可以,应该保持这种方式:)就你所知,它并不是我是谁下降了。 – Tafari

回答

0

我找到解决这个。

只需使用的onResume方法来调用适配器类...

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 

    names2 = dataManipulator.selectAll(); 

    stg1 = new String[names2.size()]; 
    int x = 0; 
    String stg; 

    for (String[] name : names2) { 
     stg = "Title : " + name[1] + "\n" + "Descr : " + name[2] + "\n" 
       + "Day's Left : " + name[3]; 
     stg1[x] = stg; 
     x++; 
    } 

    adapter = new ArrayAdapter<String>(this, R.layout.assignment_custom, 
      stg1); 
    mListView.setAdapter(adapter); 
    mListView.setCacheColorHint(Color.TRANSPARENT); 
    mListView.setBackgroundResource(R.drawable.assignment_icon); 
    adapter.notifyDataSetChanged(); 
    adapter.setNotifyOnChange(false); 
    mListView.invalidateViews(); 
} 

只需调用中的onResume方法这部分代码,就大功告成了。但无论如何,感谢所有帮助过我的人。我在这里发布这个答案,所以可能是其他人可以利用,而不是像我一样浪费时间。

0

当您删除数据时,您正在重新创建阵列。您是否尝试过重新创建适配器以使用新阵列?

+0

如果我删除我的删除方法,那么它也没有更新。 – InnocentKiller

+0

这不是我说的。 – Kuffs

2

ArrayAdapter制作您在项目中传递给它的项目的副本,并在内部使用它。

ArrayAdapter Source

所以,简单地操作您的原始数组没有任何意义的适配器。您在通知它数据已完全没有改变时发生了改变,它仍然保存着您使用的原始列表。

您需要可以再重新创建整个适配卡或使用clearaddAllremoveinsert方法来操作数据。

http://developer.android.com/reference/android/widget/ArrayAdapter.html

+0

确切地说,适配器复制并重新创建数组,以便更改原始参考不会带来任何更改。 – Techfist

相关问题