Note that there are some explanatory texts on larger screens.

plurals
  1. PORetaining items in a List View
    primarykey
    data
    text
    <p>I'm new to android development having some problems. I created a list view that is based on the user input. User has to enter a category in a dialog box and then it's added into the list. Works like a charm. The question is how do I retain those categories once the user exits from an app and starts it again ? When the user starts the app, the list is blank. Do I have to create a preference screen or something to save what the user types ? Here is my code:</p> <pre><code>public class MainActivity extends Activity { final Context context = this; ArrayAdapter&lt;String&gt; arrayAdapter; ArrayList&lt;String&gt; listItems = new ArrayList&lt;String&gt;(); ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.listView1); arrayAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, listItems); lv.setAdapter(arrayAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.menu_add_cat: LayoutInflater li = LayoutInflater.from(context); View promptAdd = li.inflate(R.layout.prompt_add, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); //set prompts.xml to alertDialogBuilder alertDialogBuilder.setView(promptAdd); final EditText etAddCat = (EditText)promptAdd.findViewById(R.id.etDialogInput); //set a dialog message alertDialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { /* * add a cat here */ String input = etAddCat.getText().toString(); if(null != input &amp;&amp; input.length() &gt; 0){ listItems.add(input); arrayAdapter.notifyDataSetChanged(); }else{ Toast.makeText(getApplicationContext(), "Please enter a new category", Toast.LENGTH_LONG).show(); } } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); break; } //return super.onOptionsItemSelected(item); return true; } }// end of MainActivity </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload