0

I have Main activity, in this activity I have one ListView with items taking from DB. Also here i have one button Add New Item. When user click on that button it open new activity. I'm opening new activity with Intent, but I'm not finishing Main activity, because the new activity is dialog and I need Main activity as a background.

Here I can add new item in DB. When item is added I'm finishing this activity. The Main activity is appearing on the screen. The problem is that the LitView is not updated, because this activity is already created before the adding the new item. So i want to reload main activity to update the ListView. But I don't know how.....or someone can give me other solution...

Populating the ListView from DB

public void PopulateListView() {
// Closing the cursor
// DEPRICATED!!
startManagingCursor(cursor);

// Set up mapping from cursor to view fields
String[] fromFieldNames = new String[] { DBAdapter.KEY_ITEM_NAME,
BAdapter.KEY_ITEM_USER, DBAdapter.KEY_ITEM_IMG, DBAdapter.KEY_ITEM_DATE_ADDED, 
DBAdapter.KEY_ITEM_FAV };

int[] toViewIDs = new int[] { R.id.itemName, R.id.itemUser,
R.id.iconCopy, R.id.date, R.id.imgFav_M };

// Create adapter to map columns of DB to elements on UI
myCursorAdapter = new SimpleCursorAdapter(
this, // Context
R.layout.item_layout, // Row layout template
cursor, // Cursor (set of DB records)
fromFieldNames, // DB column names
toViewIDs // views ID to putt in list view
);

// Set the adapter for list view
myList.setAdapter(myCursorAdapter);
}

Add new item activity

private void addItem() {

String get_name, get_username, get_pass, get_cat, get_note;

int imageID = imageIDs[random.nextInt(imageIDs.length)];

get_name = name.getText().toString();
get_username = username.getText().toString();
get_pass = pass.getText().toString();
get_cat = cat.getText().toString();
get_note = note.getText().toString();

if (get_name.equals("")) 
{
    Alert("Insert Name...");
} 
else if (get_username.equals(""))
{
     Alert("Insert Username...");
} 
else if (get_cat.equals(""))
{
myDb.insertRowInItems(get_name, get_username,
                 get_pass, "Other", get_note, imageID, isFav);  
} 
else
{
myDb.insertRowInItems(get_name, get_username,
                  get_pass, get_cat, get_note, imageID, isFav);
}

finish();
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
KiKo
  • 1,668
  • 7
  • 27
  • 56

2 Answers2

0

use notifydatasetchanged

@Override 
public void onResume(){ 
super.onResume();
this.ListAdapter.notifyDataSetChanged();
}
firozSujan
  • 118
  • 1
  • 9
0

You need to call the notifyDataSetChanged() on your ListView adapter, assuming that the collection associated with this adapter has been updated. If it hasn't, then update the underlying collection from the database, and then call notifyDataSetChanged()

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

EDIT

It seems the solution is to obtain a fresh Cursor and use either changeCursor() or swapCursor() on the CursorAdapter to affect a data change

Android SimpleCursorAdapter doesn't update when database changes

Community
  • 1
  • 1
Nathan Power
  • 630
  • 1
  • 6
  • 14
  • I'm using SimpleCursorAdapter – KiKo Apr 06 '14 at 21:15
  • So the underlying dataset is actually a database column in this case? So notifyDataSetChanged should work on it's own I think, have you tried this? – Nathan Power Apr 06 '14 at 21:23
  • Don't want to update.... here is my adapter String[] fromFieldNames = new String[] { DBAdapter.KEY_ITEM_NAME, DBAdapter.KEY_ITEM_DATE_ADDED }; int[] toViewIDs = new int[] { R.id.itemName, R.id.date } myCursorAdapter = new SimpleCursorAdapter (this, R.layout.item_layout, cursor, fromFieldNames, toViewIDs ); myCursorAdapter.notifyDataSetChanged(); // I add only this line myList.setAdapter(myCursorAdapter); – KiKo Apr 06 '14 at 21:38
  • You probably only want to call notifyDataSetChanged() when you return from your new activity. You will have to set some flag for this, and maybe put it in an intent. If you can, please edit your original post to show some more code. – Nathan Power Apr 06 '14 at 21:48
  • I think firozSujan had the right answer, override the onResume method, and call myCursorAdapter.notifyDataSetChanged() in there – Nathan Power Apr 06 '14 at 22:20
  • Try here: http://stackoverflow.com/questions/1985955/android-simplecursoradapter-doesnt-update-when-database-changes – Nathan Power Apr 06 '14 at 22:30
  • I have edited the original answer, if it reflects what solved your problem please accept as the answer? – Nathan Power Apr 07 '14 at 08:32