2013-06-05 51 views
0

我知道有很多的建議在這裏,因此及時更新列表視圖,我嘗試過,但沒有任何工程。你能幫我麼?我不知道我是否錯過了一些東西。這裏是我的代碼:如何當您添加和刪除數據庫中的項目動態更新列表視圖?

simpleAdapterConsumerList= new ConsumerList(RegistrationActivity.this, arraylistRegisteredConsumer, R.layout.registeredconsumerlistattributecell, Constants.REGISTERED_ATTRIBUTE_KEYS, Constants.REGISTERED_ATTRIBUTES_VIEWS, false); 
     lv_ConsumersList.setAdapter(simpleAdapterConsumerList); 
     registeredConsumerCount = lv_ConsumersList.getCount(); 

當我勾選提交按鈕,我想自動更新lisview而不離開頁面。而當我點擊列表視圖的警告對話框將會出現,如果用戶想刪除所選的項目,如果用戶點擊OK按鈕就在我的數據庫已經被刪除,但不更新ListView的要求。我仍然需要離開頁面,以查看更新的listview.I試圖把下面的代碼,但沒有工作。 ()

simpleAdapterConsumerList.notifyDataSetChanged(); 

((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged(); 

這裏的ID我的代碼,在數據庫

btn_Register.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Emp = txt_Emp.getText().toString(); 
      Lastname = txt_Lname.getText().toString(); 
      Firstname = txt_Fname.getText().toString(); 
      Middlename = txt_Mname.getText().toString(); 
      Cp = txt_Cp.getText().toString(); 
      Email = txt_Email.getText().toString(); 
      fullName = Lastname +" "+ Firstname +" "+Middlename; 
      careFriend = sharedPreferences.getString(Constants.SHARED_PREFERENCES_CAREFRIEND, ""); 
      companyName = sharedPreferences.getString(Constants.SHARED_PREFERENCES_COMPANY_CARE_FRIEND, ""); 
      consumercompanycode = sharedPreferences.getString(Constants.SHARED_PREFERENCES_COMPANYCODE_CARE_FRIEND, ""); 
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm"); 
      emailmark = "Not send"; 
      consumerId = sharedPreferences.getInt(Constants.SHARED_PREFERENCES_CONSUMER_ID, 1); 
      consumerId = consumerId + 1; 
      if (!Lastname.equals("")) 
      { 
       .... 
{ 
          String currentDateandTime = sdf.format(new Date()); 
          databaseAdapter.SaveConsumerDetails(new Constructor(Emp, Lastname, Firstname, Middlename, Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode)); 
          sharedPreferencesEditor.putInt(Constants.SHARED_PREFERENCES_CONSUMER_ID, consumerId); 
          sharedPreferencesEditor.commit(); 
          simpleAdapterConsumerList.notifyDataSetChanged(); 
          //((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged(); 
          //registeredConsumerCount = lv_ConsumersList.getCount(); 
          Toast.makeText(RegistrationActivity.this, "Registered successfully. " + currentDateandTime, Toast.LENGTH_LONG).show();                   simpleAdapterConsumerList.notifyDataSetChanged(); 
          Clear(); 

         }else{ 
      ....   
     } 

我的刪除按鈕添加新的數據。這裏是我的代碼

public void onClick(DialogInterface dialog, int id) 
       { 
        databaseAdapter.deleteSelectedItem(ID); 
        Toast.makeText(RegistrationActivity.this, "Delete " + ID, Toast.LENGTH_LONG).show();                    
        simpleAdapterConsumerList.notifyDataSetChanged(); 
       } 

.... 
final Cursor cursorRegisteredConsumer = databaseAdapter.getCursorRegisteredConsumer(); 

    .... 
//on my databaseAdapter here is my code 

public Cursor getCursorRegisteredConsumer(){ 
    Cursor c = dbSqlite.query(Constants.DATABASE_TABLE_CONSUMER, new String[] { Constants.DATABASE_COLUMN_ID, Constants.CONSUMER_EMPNO, Constants.CONSUMER_FIRSTNAME, Constants.CONSUMER_LASTNAME, Constants.CONSUMER_MIDDLEINITIAL, Constants.CONSUMER_CELLPHONENO, Constants.CONSUMER_EMAIL, Constants.CONSUMER_FULLNAME, Constants.CONSUMER_CAREFRIEND, Constants.CONSUMER_COMPANY, Constants.CONSUMER_REGISTRATIONDATE, Constants.CONSUMER_EMAILMARK,Constants.CONSUMER_ID,Constants.CONSUMER_COMPANYCODE}, null , null, null, null, Constants.DATABASE_COLUMN_ID + " ASC"); 
    //c.setNotificationUri(getContext().getContentResolver(), uri); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 

} 
+0

後的完整代碼顯示的刪除功能。 – lukas

+0

我已經更新了上面的代碼。 – lolliloop

回答

0

當光標適配器處理你需要調用swapCursor()並通過更新光標,然後,打電話notifyDataSetChanged

+0

我該怎麼做? – lolliloop

1

有兩個部分是:

1.備份您的適配器本身並不改變光標。

是備份您的適配器點從查詢結果集光標。當數據庫中的數據發生變化時,Cursor仍然指向相同的結果集。您需要再次查詢以獲取更新的數據。無論何時手動刪除項目,您都可以自己做這件事。

如果你已經設置了數據庫適當ContentProvider,你也可以使用一個ContentObserver和/或CursorLoader(它設置了一個ContentObserver本身),只是確保你的ContentProvider的query(...)方法你這樣做:

Cursor cursor = ...; // query 
cursor.setNotificationUri(getContext().getContentResolver(), uri); 
return cursor; 

 

2.如果你得到一個新的光標,你需要替換舊的。

你可能有一個新的光標,但你仍然適配器使用舊的,所以你需要更換。將此方法添加到適配器,並在獲得查詢結果時調用它。

public void swapCursor(Cursor newCursor) { 
    if (cursor != null && oldCursor.isOpen()) { 
     cursor.close(); 
    } 
    cursor = newCursor; 
    notifyDataSetChanged(); 
} 
0

調用notifyDataSetChanged()之前,您應該更新ConsumerList適配器的參考。
從您的代碼,我猜你用ListAdapter或者他的子類,我沒有測試其它API級別,但我相信API等級16(JELLY_BEAN)約ListAdapter.notifyDataSetChanged(不工作),即使你刷新名單。所以,我建議你使用BaseAdapter。

0

哥們,

After calling "NotifyDatasetChanged" also your listview try to access your "arraylistRegisteredConsumer" arralist. But in this ArrayList you didn't delete or insert the data. 

    That's why your listview again loading same data. 

    Perform delete operation in "arraylistRegisteredConsumer" based on "Index" then call 

    "NotifyDatasetChanged". 
相關問題