views:

60

answers:

3

I am making a very simple dump and display app. I would like to see my newly entered data displayed on the list found on the first tab. But I don't know where to place requery();

Here is the code for the first tab. (Displays the data from DB.)

public class ListTask extends ListActivity{

@Override   
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list_task);
    SQLiteDatabase db = openOrCreateDatabase("db_alist", 0, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS tbl_alist (_id INTEGER PRIMARY KEY AUTOINCREMENT, task_name VARCHAR, task_date DATE, task_details VARCHAR);");
    Cursor c = db.rawQuery("SELECT _id, task_name, task_date FROM tbl_alist", null);
    while(c.moveToNext()){
        c.getString(c.getColumnIndex("task_name"));
        c.getString(c.getColumnIndex("task_date"));
    }
    db.close();
    String[] displayfields = new String[] {"task_name", "task_date"};
    int[] displayviews = new int[] {android.R.id.text1, android.R.id.text2};
    SimpleCursorAdapter sca = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, displayfields, displayviews);
    this.setListAdapter(sca);
}
@Override
public void onResume(){
    super.onResume();
    System.out.println("Resume?");
}
@Override
public void onPause(){
    super.onPause();
    System.out.println("Paused?");
}}

I have tried doing (pseudo codes only)

public class{
Cursor c

 public void onCreate(){
 ...
 }

 public void onResume(){
 super.onResume();
 c.requery();
 }
}

and

public class{
SimpleCursorAdapter sca

 public void onCreate(){
 ...
 }

 public void onResume(){
 super.onResume();
 sca.notifyDataSetChanged();
 }
}

But BOTH don't work! Something is still wrong.

A: 

I guess onResume() will do.

This is always called immediately before the Activity becomes visible. So it is the logical place.

Promote the SimpleCursorAdapter sca to a member variable and in the onResume() method, call sca.notifyDataSetChanged()

codinguser
If I put Cursor c; before onCreate(), and put c.requery() in onResume(), this tab won't load anything!
ice3d
If you don't want to make the cursor a class field instead of local variable, then your SimpleCursorAdapter should be. In that case, you will call `notifyDataSetChanged()` on your SimpleCursorAdapter. It will handle the refreshing.
codinguser
tried putting my SimpleCursorAdapter as local variable and sca.notifyDataSetChanged() in onResume and still nothing happens.
ice3d
I am assuming by 'local variable' you mean 'class member' variable right?
codinguser
Yup. When requery, nothing shows up on the tab. When notifyDateSetChanged, current data gets shown but when new data is added from new tab, it still does not refresh.
ice3d
A: 

Finally solved my own problem.

I needed to deactivate the cursor in onPause() then call requery in onResume!

It worked. :D

ice3d
A: 

Why don't you create a ContentProvider ? With managedQuery and a ContentProvider, it would be transparent

fedj