我想保存一個列表並將其存儲在一個字符串中,並在另一個活動中獲取該字符串以顯示所有這些項目。當我使用字符串people
它沒有問題,但當我使用字符串finalp
它不起作用。 請告訴我做錯了什麼? 這個是我的MainActivity獲取項在列表中檢查到另一個活動
public class MainActivity extends ListActivity {
String finalp;
String[] people = {
"Mike Strong",
"Jennifer Anniston",
"Tom Bennet",
"Leander Paes",
"Liam Nesson",
"George Clooney",
"Barack Obama",
"Steve Jobs",
"Larry Page",
"Sergey Brin",
"Steve Wozniak"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstView = getListView();
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, people));
}
public void onListItemClick(ListView parent, View v, int position, long id){
CheckedTextView item = (CheckedTextView) v;
if(item.isChecked()){
finalp.add(position);
}else if(!item.isChecked()){
finalp.remove(position);
}
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_settings:
Intent myIntent = new Intent("com.vyprnoch.myapp1.CHECK");
startActivity(myIntent);
break;
}
return false;
}
這裏就是我的CheckedNames活動
public class CheckedNames extends MainActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstView = getListView();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, finalp));
}
}
每次這樣的項目被點擊的時候,應該得到保存的SharedPreferences? –