Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将listview中的项目签入另一个活动_Android - Fatal编程技术网

Android 将listview中的项目签入另一个活动

Android 将listview中的项目签入另一个活动,android,Android,我试图保存一个列表并将其存储在字符串中,然后在另一个活动中获取该字符串以显示所有这些项目。当我使用字符串people时,它工作正常,但当我使用字符串finalp时,它不工作。 请告诉我我做错了什么? 这是我的主要活动 public class MainActivity extends ListActivity { String finalp; String[] people = { "Mike Strong", "Jennifer Anniston",

我试图保存一个列表并将其存储在字符串中,然后在另一个活动中获取该字符串以显示所有这些项目。当我使用字符串
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;


}
public类MainActivity扩展了ListActivity{
字符串finalp;
字符串[]人={
“迈克·斯特朗”,
“珍妮弗·安妮斯顿”,
“汤姆·班纳特”,
“Leander Paes”,
“Liam Nesson”,
“乔治·克鲁尼”,
“巴拉克·奥巴马”,
“史蒂夫·乔布斯”,
“拉里·佩奇”,
“谢尔盖·布林”,
“史蒂夫·沃兹尼亚克”
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstView=getListView();
lstView.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
setListAdapter(新阵列适配器)(此,
android.R.layout.simple_list_item_checked,people));
}
public void onListItemClick(ListView父视图、视图v、整数位置、长id){
CheckedTextView项=(CheckedTextView)v;
如果(item.isChecked()){
最后添加(位置);
}如果(!item.isChecked()){
最终移除(位置);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
//TODO自动生成的方法存根
开关(item.getItemId()){
案例R.id.action\u设置:
Intent myIntent=newintent(“com.vyprnoch.myapp1.CHECK”);
星触觉(myIntent);
打破
}
返回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));

    }

}
public类CheckedNames扩展main活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstView=getListView();
setListAdapter(新阵列适配器)(此,
android.R.layout.simple_list_item_1,finalp));
}
}
有一个数组列表

    ArrayList<String> finalp = new ArrayList<String>();
然后

接受

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

    }
}
公共类CheckedNames扩展了ListActivity{
ArrayList a;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle extras=getIntent().getExtras();
如果(附加值!=null)
{
a=额外的getStringArrayList(“键”);

对于(int i=0;iSo每次单击该项时,它都应通过SharedReferences存储?第二个活动完全为空。不显示任何内容。您应该具有该活动的布局。检查日志如果arraylist传递给第二个活动,您至少应在日志中看到arraylist的内容。是,它在logcat中显示选定的项。但我想在其他活动中展示。我需要补充什么?非常感谢。我让它发挥作用。它完全按照我想要的方式发挥作用。
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

    }
}
 <?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>