2012-01-19 55 views
0

我有一個Activity,它讓用戶輸入名稱並保存它們。點擊添加Button後,該名稱將被添加到Array中,並顯示在ListView中,並且可以選擇刪除名稱。在活動中保存數組

我的問題是,我試圖去那裏,如果用戶離開Activity然後回來給ActivityArrayListView將已保存的點,所以沒有必要再次輸入所有名稱。此刻一旦Activity被留下,然後回到ArrayListView都是空的。

正如你可以看到在底部我已經嘗試使用onSaveInstanceStateonRestoreInstanceState,但不能讓他們的工作任何人都可以擴展此代碼?

ArrayList<String> playerList = new ArrayList<String>(); 
ListView employeeList; 
ArrayAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.addremove); 

final MyAdapter adapter = new MyAdapter(this, R.layout.listview_content, playerList); 
ListView employeeList = (ListView) findViewById(R.id.namelistview); 
employeeList.setAdapter(adapter); 



Button confirm = (Button) findViewById(R.id.add); 
confirm.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
EditText playername = (EditText) findViewById(R.id.userinput); 
playerList.add(playername.getText().toString()); 
adapter.notifyDataSetChanged(); 
playername.setText(""); 

}}); 

Button play = (Button) findViewById(R.id.playnow); 
play.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
Intent intent = new Intent(demo.AddRemove.this, demo.PasswActivity.class); 
intent.putStringArrayListExtra("KEY", playerList); 
startActivity(intent); 

}}); 
} 
class MyAdapter extends ArrayAdapter<String>{ 
    private OnClickListener listener; 
    public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) { 
     super(context, textViewResourceId, objects); 
     listener = new OnClickListener(){ 
      public void onClick(View v){ 
      playerList.remove(v.getTag()); 
      notifyDataSetChanged(); 

      }; 
      }; 
      } 


    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView== null) convertView = getLayoutInflater().inflate(R.layout.listview_content, null); 

     TextView myTextView1 = (TextView)convertView.findViewById(R.id.playername); 
     myTextView1.setText(getItem(position)); 
     Button remove = (Button)convertView.findViewById(R.id.remove); 
     remove.setTag(getItem(position)); 
     remove.setOnClickListener(listener); 
     return convertView; 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    // Save UI state changes to the savedInstanceState. 
    // This bundle will be passed to onCreate if the process is 
    // killed and restarted. 
    outState.putStringArrayList("Playlist", playerList); 

    super.onSaveInstanceState(outState); } 


    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState) { 
     super.onRestoreInstanceState(savedInstanceState); 
     // Restore UI state from the savedInstanceState. 
     // This bundle has also been passed to onCreate. 
     playerList = savedInstanceState.getStringArrayList("Playlist"); 


    } 

}

回答

1

你有沒有嘗試使你的playerList作爲

static 

它會解決你的目的

+0

你意味着'onSaveInstanceState'和'onRestoreInstanceState'應該是'公共靜態無效'?那只是拋出錯誤? – Matt

+0

private static ArrayList playerList = new ArrayList (); 我的意思是,如果你想保存在活動使用這個。它可以解決你的目的。直到那時我會嘗試你的代碼,並讓你知道你的代碼是什麼概率。你有沒有嘗試將它保存在你自己的sharedPreferences文件 – AAnkit

+0

好吧,不,我從來沒有真正使用過'sharedPreferences'。 – Matt