2013-04-10 62 views
0

我有一個奇怪的問題..我有一個列表視圖,其中包含複選框..我想用戶選擇它們,然後我得到選定的項目neme並將它們添加到循環..我以前ArrayAdapter做每一個認爲是好的,但是當我檢查的最後一個項目是第一次,我得到的錯誤,但如果我檢查其他複選框,第一次我因此未得到error..Here是clicklistener在列表視圖中點擊最後一個複選框時出現錯誤

package co.tosca.persianpoem; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.database.Cursor; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 
import android.widget.Toast; 

public class InteractiveArrayAdapter extends ArrayAdapter<String> { 

     private final List<String> list; 
     private final Activity context; 
     public ArrayList<String> items = new ArrayList<String>(); 
     public ArrayList<String> main_db_id = new ArrayList<String>(); 
     public List<String> database_ids = new ArrayList<String>(); 

     public InteractiveArrayAdapter(Activity context, List<String> list) { 
     super(context, R.layout.database_list_item, list); 
     this.context = context; 
     this.list = list; 
     persian_poem_class main=new persian_poem_class(context); 
     String mySQL = "SELECT DISTINCT id as _id "+","+ClubCP.COLUMN_POET_ID+","+ClubCP.COLUMN_PARENT_ID 
       + " from cat" 
       + " where " + ClubCP.COLUMN_PARENT_ID+"= 0 " 
       + " order by " + ClubCP.COLUMN_POET_ID; 
     Cursor c= main.getData(mySQL); 

     c.moveToFirst(); 
     while(!c.isAfterLast()) { 
      main_db_id.add(c.getString(c.getColumnIndex(ClubCP.COLUMN_POET_ID))); //add the item 
      c.moveToNext(); 
     } 
     Log.i("main_db_id", main_db_id.toString()); 
     File path=new File(ClubCP.SDcardPath+"/temp/database/"); 
     database_ids = main.directoryPath(path,true); 
     Log.i("database_ids", database_ids.toString()); 
     } 



     public ArrayList<String> getSelectedItems(){ 
     return items; 
     } 




     @Override 
     public View getView (int position, View convertView, ViewGroup parent) { 
      View itemView = convertView ; 
      final int pos =position; 
      if (itemView == null) { 


      LayoutInflater inflator = context.getLayoutInflater(); 
      itemView = inflator.inflate(R.layout.database_list_item, null); 

      CheckBox cb = (CheckBox)itemView.findViewById(R.id.chk_database_list_item); 
      TextView tv =(TextView)itemView.findViewById(R.id.txt_database_status); 
      Log.i("aray adapter","set caption"); 
      cb.setText(list.get(position)); 

      for(int i=0;i<database_ids.size();i++){ 
       boolean found = false; 
       for(int j=0;j<main_db_id.size();j++){ 
         if((database_ids.get(position).equals(main_db_id.get(j)))){ 
           found = true; 
           break; 
         } 
       } 
       if(found) { 
         tv.setText(context.getString(R.string.is_in_db)); 
         tv.setTextColor(Color.parseColor(context.getString(R.color.red))); 
       } else { 
        tv.setText(context.getString(R.string.is_not_in_database)); 
        tv.setTextColor(Color.parseColor(context.getString(R.color.green)));            
       } 
     } 


      cb.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        CheckBox myCb = (CheckBox)v; 

        if (myCb.isChecked()) { 
         items.add(pos, list.get(pos)); 
         Toast.makeText(context, items.toString(), 1).show(); 
        } else { 
         items.remove(pos); 
        } 
       } 
      }); 
      } 
      return itemView; 
     } 
    } 

這裏是錯誤

04-10 10:24:51.722: E/AndroidRuntime(1938): FATAL EXCEPTION: main 
04-10 10:24:51.722: E/AndroidRuntime(1938): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at java.util.ArrayList.add(ArrayList.java:143) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at co.tosca.persianpoem.InteractiveArrayAdapter$1.onClick(InteractiveArrayAdapter.java:97) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.view.View.performClick(View.java:3534) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.widget.CompoundButton.performClick(CompoundButton.java:104) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.view.View$PerformClick.run(View.java:14263) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.os.Handler.handleCallback(Handler.java:605) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.os.Looper.loop(Looper.java:137) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at android.app.ActivityThread.main(ActivityThread.java:4441) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-10 10:24:51.722: E/AndroidRuntime(1938):  at dalvik.system.NativeStart.main(Native Method) 

它說出了bundry,所以我認爲問題是w第i個項目添加在列表視圖,但不知道爲什麼:(

,這裏是一個新的logcat

04-10 20:23:14.947: D/dalvikvm(7732): GC_FOR_ALLOC freed 45K, 3% free 7646K/7875K, paused 23ms 
04-10 20:23:14.947: I/dalvikvm-heap(7732): Grow heap (frag case) to 8.968MB for 1536016-byte allocation 
04-10 20:23:14.977: D/dalvikvm(7732): GC_CONCURRENT freed <1K, 3% free 9145K/9415K, paused 1ms+1ms 
04-10 20:23:15.097: D/libEGL(7732): loaded /system/lib/egl/libGLES_android.so 
04-10 20:23:15.117: D/libEGL(7732): loaded /system/lib/egl/libEGL_adreno200.so 
04-10 20:23:15.137: D/libEGL(7732): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
04-10 20:23:15.147: D/libEGL(7732): loaded /system/lib/egl/libGLESv2_adreno200.so 
04-10 20:23:15.227: I/Adreno200-EGLSUB(7732): <ConfigWindowMatch:2078>: Format RGBA_8888. 
04-10 20:23:15.247: D/OpenGLRenderer(7732): Enabling debug mode 0 
04-10 20:23:17.087: D/dalvikvm(7732): GC_FOR_ALLOC freed 13K, 3% free 9167K/9415K, paused 12ms 
04-10 20:23:17.097: I/dalvikvm-heap(7732): Grow heap (frag case) to 10.454MB for 1536016-byte allocation 
04-10 20:23:17.127: D/dalvikvm(7732): GC_CONCURRENT freed <1K, 3% free 10667K/10951K, paused 2ms+5ms 
04-10 20:23:17.177: D/TextLayoutCache(7732): Using debug level: 0 - Debug Enabled: 0 
04-10 20:23:17.197: I/Adreno200-EGLSUB(7732): <ConfigWindowMatch:2078>: Format RGBA_8888. 
04-10 20:23:17.338: D/OpenGLRenderer(7732): Flushing caches (mode 0) 
04-10 20:23:19.057: I/file_list(7732): [khiam.db, prvin-aEtsami.db] 
04-10 20:23:19.057: I/main_db_id(7732): [3] 
04-10 20:23:19.077: I/database_ids(7732): [3, 8] 
04-10 20:23:19.087: I/aray adapter(7732): set caption 
04-10 20:23:19.107: I/Adreno200-EGLSUB(7732): <ConfigWindowMatch:2078>: Format RGBA_8888. 
04-10 20:23:19.117: I/aray adapter(7732): set caption 
04-10 20:23:19.137: I/aray adapter(7732): set caption 
04-10 20:23:19.297: D/OpenGLRenderer(7732): Flushing caches (mode 0) 
04-10 20:23:20.387: I/getSelectedItems_befor(7732): [] 
04-10 20:23:20.387: I/list(7732): [khiam.db, prvin-aEtsami.db] 
04-10 20:23:20.387: D/AndroidRuntime(7732): Shutting down VM 
04-10 20:23:20.387: W/dalvikvm(7732): threadid=1: thread exiting with uncaught exception (group=0x2b542210) 
04-10 20:23:20.397: E/AndroidRuntime(7732): FATAL EXCEPTION: main 
04-10 20:23:20.397: E/AndroidRuntime(7732): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at java.util.ArrayList.add(ArrayList.java:143) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at co.tosca.persianpoem.InteractiveArrayAdapter$1.onClick(InteractiveArrayAdapter.java:102) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.view.View.performClick(View.java:3534) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.widget.CompoundButton.performClick(CompoundButton.java:104) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.view.View$PerformClick.run(View.java:14263) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.os.Handler.handleCallback(Handler.java:605) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.os.Looper.loop(Looper.java:137) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at android.app.ActivityThread.main(ActivityThread.java:4441) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-10 20:23:20.397: E/AndroidRuntime(7732):  at dalvik.system.NativeStart.main(Native Method) 
+0

'main_db_id'的結構是什麼? – 2013-04-10 06:12:11

+0

@PareshMayani我更新了我的問題..謝謝 – Majid 2013-04-10 06:14:47

回答

2
items = (items == null) ? new ArrayList<Object>(list.size) : items; 

做到這一點,而初始化的項目。希望這會有所幫助。您正試圖向ArrayList添加一個物品,該物品沒有您在add命令中提及的位置。嘗試將上面的行添加爲getView方法中的第一條語句。而且也可以使用items.set(pos, list.get(pos));

+0

我試過這個,但我得到「空白最終字段列表可能沒有初始化」 – Majid 2013-04-10 06:17:24

+0

它應該在裏面getView方法 – Triode 2013-04-10 06:22:30

+0

它工作?有人在嗎 ? – Triode 2013-04-10 06:49:19

相關問題