Android 当用户单击项目时,在两个列表视图之间移动项目会使应用程序停止

Android 当用户单击项目时,在两个列表视图之间移动项目会使应用程序停止,android,android-listview,android-arrayadapter,Android,Android Listview,Android Arrayadapter,我正在尝试在两个列表视图之间移动一个项目(一个包含选中的项目,另一个包含未选中的项目)。因此,单击某个项目时,其选中状态已更改,因此将其移动到另一个listview。但它不起作用 Java代码: public class T_C_Activity extends Activity { /** * Called when the activity is first created. */ //private EditText edittext; pri

我正在尝试在两个列表视图之间移动一个项目(一个包含选中的项目,另一个包含未选中的项目)。因此,单击某个项目时,其选中状态已更改,因此将其移动到另一个listview。但它不起作用

Java代码:

public class T_C_Activity extends Activity {
    /**
     * Called when the activity is first created.
     */

    //private EditText edittext;
    private Button button;
    private  ArrayAdapter<String> adapter;
    private  ArrayAdapter<String> adapter1;

    private boolean[] check = new boolean[10];

    String[] countries = new String[]{
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView lvc = (ListView) findViewById(R.id.lvc);

        ListView lvnc = (ListView) findViewById(R.id.lvnc);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, countries);
        //lvnc.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        lvnc.setAdapter(adapter); lvnc.setAdapter(adapter);

        adapter1 = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_multiple_choice, new String[0]);

                lvc.setAdapter(adapter1);

        lvnc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //To change body of implemented methods use File | Settings | File Templates.
                String s = adapter.getItem(i);
                adapter1.add(s);
                adapter.remove(s);
                adapter1.notifyDataSetChanged();
                adapter.notifyDataSetChanged();
            }
        });

        lvc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //To change body of implemented methods use File | Settings | File Templates.
                String s = adapter.getItem(i);
                adapter.add(s);
                adapter1.remove(s);
                adapter.notifyDataSetChanged();
                adapter1.notifyDataSetChanged();
            }
        });

    }
}

单击lvnc(ListView)中的项目时,应用程序停止。怎么了?另外,我看不到两个ListView之间的分离。谢谢。

问题在于,您正在使用简单的字符串数组初始化两个
ArrayAdapter
s,它们不支持动态添加/删除元素

相反,您可以给每个
ArrayAdapter
一个
ArrayList

ArrayList<String> countryArrayList = new ArrayList<String>(countries);
adapter = new ArrayAdapter<String>(this, ..., countryArrayList);
lvnc.setAdapter(adapter); // Note: your code does this line twice

ArrayList<String> secondArrayList = new ArrayList<String>();
adapter1 = new ArrayAdapter<String>( this, ..., secondArrayList);
ArrayList countryArrayList=新的ArrayList(国家);
适配器=新的ArrayAdapter(此,…,countryArrayList);
lvnc.setAdapter(适配器);//注意:您的代码在这一行中执行了两次
ArrayList secondArrayList=新的ArrayList();
adapter1=新的ArrayAdapter(此,…,第二个ArrayList);

要操作列表,请在
countryArrayList
secondArrayList
上根据需要调用
.add()
删除()
,然后在适配器上调用
notifyDataSetChanged()

阵列不是动态的。您不能从中添加和删除项目

更改:

String[] countries = new String[]{
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal",
            "Afghanistan",
            "North Korea",
            "South Korea",
            "Japan"
    };

ArrayList<String> countries = new ArrayList<String>();
            countries.add("India");
            countries.add("Pakistan");
            countries.add("Sri Lanka");
            countries.add("China");
            countries.add("Bangladesh");
            countries.add("Nepal");
            countries.add("Afghanistan");
            countries.add("North Korea");
            countries.add("South Korea");
            countries.add("Japan");
adapter1 = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>());
ArrayList countries=new ArrayList();
国家。添加(“印度”);
国家。添加(“巴基斯坦”);
国家。添加(“斯里兰卡”);
国家。添加(“中国”);
国家。添加(“孟加拉国”);
国家。添加(“尼泊尔”);
国家。添加(“阿富汗”);
国家。添加(“朝鲜”);
国家。添加(“韩国”);
国家。添加(“日本”);

adapter1 = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_multiple_choice, new String[0]);
adapter1=newarrayadapter(这是android.R.layout.simple\u list\u item\u多项选择,新字符串[0]);

ArrayList<String> countries = new ArrayList<String>();
            countries.add("India");
            countries.add("Pakistan");
            countries.add("Sri Lanka");
            countries.add("China");
            countries.add("Bangladesh");
            countries.add("Nepal");
            countries.add("Afghanistan");
            countries.add("North Korea");
            countries.add("South Korea");
            countries.add("Japan");
adapter1 = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>());
adapter1=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u多选,new ArrayList());

请发布故障的Logcat输出。