Listview没有使用notifyDataSetChanged方法进行更新

时间:2013-12-10 11:24:26

标签: android listview android-arrayadapter

我正在尝试使用notifyDataSetChanged()这个方法更新我的listview,但listview没有得到更新,如果我按下后退按钮并转到上一个活动然后再次如果我进入此活动然后它正在更新,我不知道为什么。我尝试了所有可能的解决方案但没有得到适当的解请帮助下面是我的代码。

这是我试过的链接

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();
}
}

3 个答案:

答案 0 :(得分:2)

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

ArrayAdapter Source

因此,简单地操作原始数组对适配器没有任何意义。您正在通知它数据在完全没有更改时已更改,它仍保留您填充的原始列表。

您需要重新创建整个adpater,或使用clearaddAllremoveinsert方法来操作数据。

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

答案 1 :(得分:0)

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

答案 2 :(得分: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方法中调用此部分代码即可完成。但无论如何,感谢所有帮助过我的人。我在这里发布这个答案,所以可能是其他人可以受益,而不是像我一样浪费时间。