Reputation: 399
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;
}
Upvotes: 0
Views: 503
Reputation: 31
Dude,
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".
Upvotes: 0
Reputation: 3827
before you invoke notifyDataSetChanged(), you should update the ConsumerList referenced of Adapter.
from your code, I guess you used ListAdapter or his subclasses, I didn't test for other API Level, but I believe the API Level 16(JELLY_BEAN) doesn't work about ListAdapter.notifyDataSetChanged() even if you refresh the List. so, I advice you use BaseAdapter.
Upvotes: 0
Reputation: 38605
There are two parts to this:
1. The Cursor that backs your adapter doesn't change by itself.
The Cursor that backs your adapter points to a set results from a query. When the data in the database changes, the Cursor still points to the same result set. You need to query again to get updated data. You can do this yourself whenever you manually delete an item.
If you've set up a proper ContentProvider
for your database, you could instead use a ContentObserver
and/or a CursorLoader
(which sets up a ContentObserver
itself) and just make sure that in your ContentProvider's query(...)
method you do this:
Cursor cursor = ...; // query
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
2. If you get a new Cursor, you need to replace the old one.
You may have a new cursor, but your adapter still uses the old one, so you need to replace them. Add this method to your adapter and call it whenever you get the result of a query.
public void swapCursor(Cursor newCursor) {
if (cursor != null && oldCursor.isOpen()) {
cursor.close();
}
cursor = newCursor;
notifyDataSetChanged();
}
Upvotes: 1
Reputation: 11538
When dealing with Cursor Adapters you need to call swapCursor()
and pass the updated cursor, then, call notifyDataSetChanged
.
Upvotes: 0