2013-06-26 55 views
0

我是新來的Android開發有一些問題。我創建了一個基於用戶輸入的列表視圖。用戶必須在對話框中輸入一個類別,然後將其添加到列表中。奇蹟般有效。問題是,一旦用戶退出應用程序並重新啓動應用程序,我該如何保留這些類別?當用戶啓動應用程序時,列表是空白的。我是否必須創建一個首選項屏幕或其他內容來保存用戶輸入的內容?這裏是我的代碼:在列表視圖中保留項目

public class MainActivity extends Activity { 

final Context context = this; 
ArrayAdapter<String> arrayAdapter; 
ArrayList<String> listItems = new ArrayList<String>(); 
ListView lv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    lv = (ListView)findViewById(R.id.listView1); 
    arrayAdapter = new ArrayAdapter<String>(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 && input.length() > 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 

回答

1

你可以將它保存在SQLite數據庫中,使用CursorAdapter作爲你的列表視圖。

0

如果要保存的數據量相對較小,可以使用SharedPreferencesString數據保存在onClick方法中。

@Override 
public void onClick(DialogInterface dialog, int which) { 

    String input = etAddCat.getText().toString(); 
    if(null != input && input.length() > 0){ 
     listItems.add(input); 

     // Add all string data to List<String> listItem 
     listItem.add(input); 

     arrayAdapter.notifyDataSetChanged(); 

    }else{ 
     Toast.makeText(getApplicationContext(), "Please enter a new category", Toast.LENGTH_LONG).show(); 
    } 
} 

當用戶離開你的活動,使用onStop()回調方法,以節省您的List<Strings>並將其存儲通過SharedPreferences

@Override 
private void onStop() { 
    super.onStop(); 

    SharedPreferences.Editor editor = mSharedPreferences.edit(); 

    editor.putString(getResources().getString(R.string.list_of_strings), new HashSet<String>(listItem)); 
    editor.commit; 
} 

使用onStart()回調,初始化ListSharedPreferences。當用戶導航到您的活動時,您的列表將在通過onStop()保存時重新初始化。

最後,遍歷您的列表,將您的項目添加到您的ArrayList', create your ArrayAdapter`並將其設置爲您的列表。

@Override 
private onStart(){ 
    super.onStart(); 

    SharedPreferences mSharedPreferences; 

    mSharedPreferences = this.getApplicationContext().getSharedPreferences("MyPreferences", 0); 
    List<String> listItems = new ArrayList<String>(mSharedPreferences.getStringSet("ListOfStrings", null)); 

    ListIterator li = listItem.listIterator(0); 

    while (li.hasNext()) { 
     newStatusList.add((String)li.next()); 
    } 

    arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems); 
    lv.setAdapter(arrayAdapter); 
}