我正在尝试保存列表并将其存储在字符串中,并在另一个活动中获取该字符串以显示所有这些项目。当我使用字符串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));
}
}
答案 0 :(得分:2)
拥有一个ArrayList
ArrayList<String> finalp = new ArrayList<String>();
然后
public void onListItemClick(ListView parent, View v, int position, long id){
CheckedTextView item = (CheckedTextView) v;
if(item.isChecked()){
finalp.add(people[position]);
}else if(!item.isChecked()){
finalp.remove(position);
}
}
然后
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_settings :
Intent myIntent = new Intent(this,SecondActivity.class);
myIntent.putExtra("key",finalp);
startActivity(myIntent);
break;
}
return false;
}
收到
public class CheckedNames extends ListActivity{
ArrayList<String> a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
a = extras.getStringArrayList("key");
for(int i=0;i<a.size();i++)
{
Log.i("...........",a.get(i));
}
}
ListView lstView = getListView();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, a));
// do whatever is needed with arraylist a
}
}
定义second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>