views:

51

answers:

1

I obviously new and have been trying to two days to figure out how to save the state of my main activity to no avail. I would appreciate any help. When I launch the ShowDetail activity and return to the main activity I have no data in the list. I have two xml files a main.xml and a item.xml file. main is just a listview and a textview. Item.xml is 3 textviews for the data in the list. Item Here is the code from my main activity:

public class main extends ListActivity {

private EventsData events;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    events = new EventsData(this);
    try {
     Cursor cursor = getEvents();
     showEvents(cursor);
    } finally {
     events.close();
    }
}

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

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

@Override
public void onPause(){
 super.onPause();
}

@Override
public void onRestart(){

 super.onRestart();

}


private static String[] FROM = { CODE, EXCERPT, _ID, };
private static String ORDER_BY = CODE + " ASC";
private Cursor getEvents() {
 SQLiteDatabase db = events.getReadableDatabase();
 Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
 startManagingCursor(cursor);
 return cursor;
}

private static int[] TO = { R.id.code, R.id.excerpt, };
private void showEvents(Cursor cursor) {
 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
   R.layout.item, cursor, FROM, TO);
 setListAdapter(adapter);
}

private static String[] MIKEY = { _ID, CODE, DEFINITION };
protected void onListItemClick(ListView l, View v, int position, long id) {
 Cursor cursor = ((CursorAdapter)getListAdapter()).getCursor();
 cursor.getLong(2);

 SQLiteDatabase db = events.getReadableDatabase();
 Cursor c = db.query(TABLE_NAME, MIKEY, "_id = "+cursor.getLong(2)+"", null, null, null, null);
 c.moveToFirst();

 Intent in1  = new Intent();
 Bundle bun = new Bundle();
 bun.putLong("id", c.getLong(0));
 bun.putString("code", c.getString(1));
 bun.putString("definition", c.getString(2));
 in1.setClass(this, ShowDetail.class);
 in1.putExtras(bun);

 startActivity(in1);
}

}

A: 

Hi,

I'd say you need to place your general actions into onResume() instead of in onCreate().

Maybe a look at the application lifecycle helps understanding what I mean: http://developer.android.com/reference/android/app/Activity.html

Sotapanna
I've read this over and over and it hasn't helped me with my problem. I can see how easy it is to save the state of textviews, etc but not a listview. Thanks for the response.
Michael Little
I just read your answer again and if I put my general actions in onresume it will fc because nothing has been saved. thanks again for the response.
Michael Little
Could you post the Stacktrace? So, you want to fill your listview and keep the selected item, right?
Sotapanna
yes. that is exactly what I want to do.
Michael Little
Sotapannn, your answer was correct. I was calling onResume() incorrectly and tried it again this morning and got it to work. Thanks for the help
Michael Little
Great! Just deleted the test project... ;)
Sotapanna