我知道这里有很多关于更新列表视图的建议,我已经尝试过了,但没有任何效果。你能帮我么?我不知道我是否错过了什么。这是我的代码:
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。当我点击 listview 时,会出现一个警告对话框,询问用户是否要删除所选项目,如果用户点击 OK 按钮,它已经在我的数据库中被删除,但 listview 没有更新。我仍然需要离开页面才能看到更新的列表视图。我尝试将代码放在下面,但没有任何效果。()
simpleAdapterConsumerList.notifyDataSetChanged();
和
((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged();
这里是我在数据库中添加新数据的代码
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;
}