Reputation: 5
I need to save the state of listview when the application closes (after turning the screen). SQL does not want to use. I know that there are standard methods: SharedPreferences, onSaveInstanceState. My code is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------listitem------------
ListView listView = (ListView) findViewById(R.id.listView1);
catnames = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, catnames);
listView.setAdapter(adapter);
listView.setAdapter(adapter);
}
And put Adapter
catnames.add(0, name);
adapter.notifyDataSetChanged();
Maybe I should not use ArrayAdapter? Help please ... I want to save catnames) ps application extends SherlockActivity.
Upvotes: 0
Views: 773
Reputation: 24998
You need to use the SQLite
datababse to persist the data. When the application closes, save the data to the database. When the application starts, retrieve the data from the database. All you need to learn is simple SQL :)
This should give you a head start: http://www.vogella.com/tutorials/AndroidSQLite/article.html
Update:
If you are looking to use onSavedInstanceState()
then you need to use Bundle
s which are, in simple terms, key-value pairs.
Once you get your adapter to populate the data, just use the setSelection()
or the smoothScrollToPosition()
to go to the element you want. This implies that you current position of the list when your screen goes off. For that, you need the getFirstVisiblePosition()
method. This will be saved in your Bundle
and this is what you will use when your screen comes back on.
Upvotes: 1