当用户点击项目使应用停止时,在两个列表视图之间移动项目

时间:2012-11-01 02:19:12

标签: android android-listview android-arrayadapter

我正在尝试在两个列表视图之间移动一个项目(一个包含已检查的项目,另一个包含未检查的项目)。因此,当单击某个项目时,其已检查状态已更改,因此将其移动到其他列表视图。但它没有用。

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)中的项目时,应用程序将停止。怎么了?此外,我没有看到两个列表视图之间的分离。感谢。

2 个答案:

答案 0 :(得分: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>());

答案 1 :(得分:0)

问题是你是用简单的字符串数组初始化两个ArrayAdapter,它们不支持动态添加/删除元素。

相反,您可以为每个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()上调用countryArrayListsecondArrayList,然后在适配器上调用notifyDataSetChanged()