我知道有很多的建議在這裏,因此及時更新列表視圖,我嘗試過,但沒有任何工程。你能幫我麼?我不知道我是否錯過了一些東西。這裏是我的代碼:如何當您添加和刪除數據庫中的項目動態更新列表視圖?
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;
}
後的完整代碼顯示的刪除功能。 – lukas
我已經更新了上面的代碼。 – lolliloop