使用notifyDataSetChanged();从其他活动和同一活动内部更新SQLite之后

时间:2016-10-11 07:38:06

标签: android sqlite listview

摘要:我正在创建一个以SQLite作为数据库的示例备忘录应用程序。

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);    

    lv = (ListView)findViewById(R.id.memo_list_view);

    Cursor c = SplashActivity.db.rawQuery("SELECT * FROM memos", null);
    if(c.getCount()!=0) {
        while(c.moveToNext()) {

            memoDetails = new MemoDetails();

            memoDetails.setmMemoId(c.getString(0));
            memoDetails.setmMemoTitle(c.getString(1));
            memoDetails.setmCreatedDate(c.getString(3));
            memoDetails.setmAlarmedDate(c.getString(4));
            memos.add(memoDetails);

        }
        c.close();
        customAdapter = new CustomAdapter(this, memos);
        lv.setAdapter(customAdapter);
    }

@Override
protected void onResume() {
    super.onResume();
    customAdapter.notifyDataSetChanged();
}

然后这是我添加备忘录的活动

AddMemoActivity (这是我使用的监听器)

 saveMemo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v1) {
            if(memoTitle.getText().toString().trim().length() == 0 ||
                memoContent.getText().toString().trim().length() == 0) {
                showMessage("Error", "Please Enter All Values");    
            } else {

                SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss");
                String currentDateandTime = sdf.format(new Date());

                SplashActivity.db.execSQL("INSERT INTO memos VALUES(null,'" 
                        + memoTitle.getText() + "','" 
                        + memoContent.getText() + "','" 
                        + currentDateandTime + "','"
                        + currentDateandTime + "','0');");

            showMessage("Success", "Memo is Saved.");   
            }
        }
    }); 

在这个“添加备忘录”部分,我的问题是当我离开 AddMemoActivity 并返回 MainActivty 时,它不会更新我的列表视图。

其次是我的删除部分,

MainActivity

 findViewById(R.id.del_memo).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v1) {              
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        String deleteIds = null;
                        for (MemoDetails p : customAdapter.getBox()) {
                            if (p.mCheckBox){
                                if(deleteIds == null) {
                                    deleteIds = p.getmMemoId();
                                } else {
                                    deleteIds += ", " + p.getmMemoId();
                                }

                            }
                        }

                        SplashActivity.db.execSQL("DELETE FROM memos WHERE id IN ('"+deleteIds+"')");
                        customAdapter.notifyDataSetChanged();

                         Toast.makeText(getApplicationContext(), deleteIds,
                                   Toast.LENGTH_LONG).show();
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(MemoListActivity.this);
            builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();  
        }
    });       

如您所见,删除后我已经添加了 customAdapter.notifyDataSetChanged(); ,但仍然没有更新。

提前致谢。

2 个答案:

答案 0 :(得分:0)

Listview不会通过更新DB来更新。您需要更新为适配器提供的列表,然后调用notifyDataChange()。

每次更改数据库数据时,请更新列表视图的udapter列表。

只需更新memos列表,然后调用通知数据更改。

答案 1 :(得分:0)

notifyDataSetChanged无济于事,您需要更新传递适配器的列表。我使用SugarORM进行SQL操作,所以它看起来像我这样:

使用数据初始填充适配器:

for (User user : SugarRecord.listAll(User.class)) {
    userNames.add(user.getName());
}
swipeActionAdapter = new SwipeActionAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, userNames));

然后添加或删除用户后:

userNames.clear();
for (User user : SugarRecord.listAll(User.class)) {
        userNames.add(user.getName());
}
swipeActionAdapter.notifyDataSetChanged();