2016-10-11 15 views
0

摘要:我正在創建一個示例備忘錄應用程序,SQLite作爲其數據庫。使用notifyDataSetChanged();從其他活動更新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."); 
      } 
     } 
    }); 

在此「添加活動備忘錄「部分,我的問題是當我離開我的AddMemoAct ivity並返回到我的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();刪除後仍然沒有更新。

在此先感謝。

回答

0

Listview不通過更新數據庫進行更新。您需要更新您給適配器的列表,然後調用notifyDataChange()。

因此,每次更改DB數據時,更新列表視圖的udapter列表。

只需更新memos列表,然後調用通知數據更改。

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