单击列表视图中的最后一个复选框时出错

时间:2013-04-10 06:08:54

标签: android android-listview

我有一个奇怪的问题..我有一个列表视图,其中包含复选框..我希望用户选择它们然后我选择项目neme并将它们添加到循环中..我使用ArrayAdapter来执行此操作每个想法都很好但是当我第一次检查最后一项时我得到错误但如果我第一次检查其他复选框我没有得到错误..这是在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)

它说出了捆绑,所以我认为问题是在listview中添加项目,但不知道原因:(

这是一个新的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)

1 个答案:

答案 0 :(得分:2)

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

初始化项目时执行此操作。希望这会有所帮助。您正尝试将项目添加到ArrayList,而add没有您在getView命令中提到的位置。尝试将上面的行添加为items.set(pos, list.get(pos));方法中的第一个语句。而且不是添加使用{{1}}