2012-11-01 63 views
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控件)的項目被點擊應用程序停止。哪裏不對?另外,我沒有看到兩個列表視圖之間的分離。謝謝。

+0

請從崩潰發佈Logcat輸出。 – acj

+0

已發佈Logcat輸出 – mj1261829

回答

0

問題是,您正在使用簡單的字符串數組初始化兩個ArrayAdapter,它們不支持動態添加/移除元素。

相反,你可以給每個ArrayAdapterArrayList

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

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>()); 
相關問題