0

我正在尝试在两个列表视图之间移动一个项目(一个包含选中的项目,另一个包含未选中的项目)。所以当一个项目被点击时,它的选中状态已经改变,所以它被移动到另一个列表视图。但它不起作用。

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

    }
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

<ListView
        android:id="@+id/lvc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice"
        />

    <ListView
            android:id="@+id/lvnc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:choiceMode="multipleChoice"
            />

    </LinearLayout>

这是崩溃输出:

11-01 04:39:15.478: ERROR/AndroidRuntime(613): FATAL EXCEPTION: main
        java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
        at com.app.T_C_Activity$1.onItemClick(T_C_Activity.java:56)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
        at android.widget.AbsListView$1.run(AbsListView.java:3533)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

当单击 lvnc (ListView) 中的项目时,应用程序停止。怎么了?此外,我没有看到两个列表视图之间的分离。谢谢。

4

2 回答 2

1

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

改变:

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 String[0]);

adapter1 = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>());
于 2012-11-01T02:57:08.873 回答
0

问题是您正在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);

要操作列表,请根据需要调用和.add(),然后调用适配器。.remove()countryArrayListsecondArrayListnotifyDataSetChanged()

于 2012-11-01T02:56:44.373 回答