I know there's a lot of suggestion here in SO to update the listview, I've tried them but nothing works. Can you please help me? I don't know if I missed something. Here is my code:
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();
When I tick the submit button i want to automatically update the lisview without leaving the page. And when I clicked listview an alert dialog will appear asking if the user wants to delete the selected item, if the user clicked the OK button it was already deleted in my database but the listview is not updated. I still need to leave the page to see the updated listview.I tried to put codes below but nothing works. ()
simpleAdapterConsumerList.notifyDataSetChanged();
and
((BaseAdapter) lv_ConsumersList.getAdapter()).notifyDataSetChanged();
here id my code that add new data in the database
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{
....
}
on my delete button. here is my code
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;
}