1

I am currently unable to view newly inserted sqlite data from my ListView without restarting my whole application. Currently when a ListView item is selected, a new activity starts showing detailed information. once the detailed info is updated, edited, or the item is deleted the ListView is shown again but contain all original data unless app it closed and restarted. list view OnResume method is below. How do I refresh ListView when ever it is shown on screen. my attempts on adding .notifyDataSetChanged was unsuccessful. Not sure I implemented it correctly.

    public List<String> populateList (){

List<String> webNameList = new ArrayList<String>();

dataStore openHelperClass = new dataStore (this);

SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null,  null, null, dataStore.COLUMN_NAME_SITE, null);

startManagingCursor(cursor);

while (cursor.moveToNext()){

String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

LoginDetails lpDetails = new LoginDetails();

    lpDetails.setsName(sName);
    lpDetails.setwUrl(wUrl);
    lpDetails.setuName(uName);
    lpDetails.setpWord(pWord);
    lpDetails.setlNotes(lNotes);

    loginArrayList.add(lpDetails);
    webNameList.add(sName);
 }

   sqliteDatabase.close();
   return webNameList;
 }



  @Override
   protected void onResume() {
   super.onResume();
   loginListAdapter = new ArrayAdapter<String>(this,        android.R.layout.simple_list_item_1, populateList());
    loginList.setAdapter(loginListAdapter);
 }

  @Override
   public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
   Toast.makeText(getApplicationContext(), "Selected ID :" + arg2,     Toast.LENGTH_SHORT).show();

Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);

LoginDetails clickedObject = loginArrayList.get(arg2);

    Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",clickedObject.getsName());
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
loginBundle.putString("clickedUserName",clickedObject.getuName());
loginBundle.putString("clickedPassWord",clickedObject.getpWord());
loginBundle.putString("clickedNotes",clickedObject.getlNotes());

updateDeleteLoginInfo.putExtras(loginBundle);

startActivity(updateDeleteLoginInfo);
sean
  • 133
  • 8
  • 20
  • what is `dataStore.COLUMN_NAME_SITE` in your code? And how do you add//update/delete items? I think your issue is because you use both getReadableDatabase and getWriteableDatabase methods. – vortexwolf Jan 08 '13 at 20:23
  • please start by putting your database related code in a seperate class than the activity class – Mouna Cheikhna Jan 08 '13 at 20:55

3 Answers3

0

You're using startActivity. Why don't you use startActivityForResult instead? The answer to this question details how you would call the second Activity. Once it returns, you would have to use the notifyDataSetChanged() call.

Community
  • 1
  • 1
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • It has nothing to do with activities. It is clear that the main issue is the synchronization of data. – vortexwolf Jan 08 '13 at 20:27
  • Well, I see he's calling updateDeleteLoginInfo with a startActivity call. From that, I'm assuming that that's the name of the Activity he's using to update/delete the info, and since he's not blocking the current Activity, any changes to the List wouldn't be reflected. I have similar code and had similar issues and that's why I answered him. – DigCamara Jan 08 '13 at 20:33
  • @DigCamera He uses the startActivity method which is enough. It works the same as startActivityForResult if to store data in a global object which is accessible by all activities. And the SQLite database is a good example of such object, though it is more difficult to use than some in-memory ArrayList. – vortexwolf Jan 08 '13 at 20:40
  • @vorrtex The way I see it is: 1. He populates the ListView 2. He calls the Activity 3. He returns from the Activity thinking the list should be re-populated. The ListView (as far as I can tell) isn't directly linked to the query in any way. Therefore, it doesn't refresh. If he called an Activity for result he'd be able to refresh the data via a simple notifyDataSetChanged() call. – DigCamara Jan 08 '13 at 22:23
0

startManagingCursor is deprecated. Use CursorLoader instead.

It's very simple to use and updates your data in the ListView immediately when the data of the ContentProvider changes.

Here is a nice example: http://www.vogella.com/articles/AndroidSQLite/article.html#todo_activities

Johannes Staehlin
  • 3,680
  • 7
  • 36
  • 50
0

You are only showing part of the app code, so I may be offbase here, but it appears you are using a Cursor to return a List and use the List to create an ArrayAdapter...

If you want the data to be 'fresh', why not use a CursorLoader with the LoaderManager? There are several benefits to the LoaderManager (like moving the query off of the UI thread), and the LoaderManager does a bit of the heavy lifting for you when it comes to monitoring the DB for changes.

There is a blog post that I've found quite helpful - http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

user1757442
  • 326
  • 2
  • 5