0

我正在尝试保存一个列表并将其存储在一个字符串中,然后在另一个活动中获取该字符串以显示所有这些项目。当我使用字符串people时它很好,但是当我使用字符串finalp时它不起作用。请告诉我我做错了什么?这是我的主要活动

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));

    }

}
4

1 回答 1

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>
于 2013-07-02T17:17:17.230 回答