I’m trying move an item between two list views (one contains the checked items and the other the nonchecked items). So when an item is clicked its checked state has changed so it is moved to the other listview. But it is not working.
Java code:
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>
Here is the crash output:
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)
When an Item in the lvnc (ListView) is clicked the app stops. What is wrong? Also, I do not see a separation between the two listviews. Thanks.
The problem is that you’re initializing the two
ArrayAdapters with simple string arrays, which do not support dynamic adding/removing of elements.Instead, you can give each
ArrayAdapteranArrayList:To manipulate the lists, call
.add()and.remove()oncountryArrayListandsecondArrayListas appropriate, and then callnotifyDataSetChanged()on the adapters.