List View custom adapter randomly checking values

吃可爱长大的小学妹 提交于 2019-12-24 03:54:11

问题


I have made a list view with a custom Adapter having an imageview, textview and a checkBox

---------------------------------------
<ImageView --- textView --- checkbox >
---------------------------------------

The listview has an onClickListener to it and the checkboxes have onCheckListener..

I have used a boolean array (named as status) to set the checkboxes checked or not initialized to false in the begining.

Suppose I have 19 items on the list and I check the checkbox of first item it works properly and index 0 of the boolean array is changed to true. But when I scroll and check the last item 3rd position of the list is reflected and index 3 of the boolean array turns true.

Also many other checkboxes are automatically checked.

Java Code

public class Search_list extends Activity {

static String str1 = "v1", str2 = "v2";
String s, item, idd;
static int spnr, range1, range2;
private CarHelper dbcarhelper = null;
private Cursor ourCursor;
CarHelper h = null;
List<String[]> names2 = null;
ListView LV;
MyCustomAdapter dataAdapter = null;
private ArrayList<Boolean> status;

byte[] Image = null;
Cursor c;
Bitmap decodedByte;

String c1_make = null, c1_model = null, c1_ver = null, c2_make = null,
        c2_model = null, c2_ver = null;

int cntr = 0;

Button b1;

CheckBox cb;

protected ProgressDialog dialog;

List<String[]> mlist = new ArrayList<String[]>();
TextView tv1;

ArrayList<String> stg1;

ArrayList<String> arr_make = new ArrayList<String>();
ArrayList<String> arr_model = new ArrayList<String>();
ArrayList<String> arr_ver = new ArrayList<String>();
ArrayList<Bitmap> arr_img = new ArrayList<Bitmap>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_list);

    dialog = ProgressDialog.show(Search_list.this, "Loading",
            "Please Wait...");

    status = new ArrayList<Boolean>(); /

    LV = (ListView) findViewById(R.id.list);

    //------------------ Compare button onClick() ------------------------------

    b1 = (Button) findViewById(R.id.btnCmp);
    b1.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {

            int count = 1;

            if (cntr == 1) {
                Toast.makeText(getApplicationContext(),
                        "Please select atleast two cars!",
                        Toast.LENGTH_LONG).show();
            } else {

                for (int i = 0; i < status.size(); i++) {

                    System.out.println("Compare click Position= " + i + " Status: "
                            + status.get(i));
                    // System.out.println();

                    if (status.get(i)) {

                        if (count == 2) {
                            c2_make = arr_make.get(i);
                            c2_model = arr_model.get(i);
                            c2_ver = arr_ver.get(i);

                            count++;
                        }

                        if (count == 1) {
                            c1_make = arr_make.get(i);
                            c1_model = arr_model.get(i);
                            c1_ver = arr_ver.get(i);

                            count++;
                        }

                    } else {

                    }

                }

                Intent intent = new Intent(Search_list.this, DispComp.class);
                Bundle b = new Bundle();

                b.putString("car1", c1_make);
                b.putString("carm1", c1_model);
                b.putString("carv1", c1_ver);

                b.putString("car2", c2_make);
                b.putString("carm2", c2_model);
                b.putString("carv2", c2_ver);

                intent.putExtras(b);
                startActivity(intent);
            }
        }
    });

    //--------------------------------------------------------------------------

    dbcarhelper = new CarHelper(this);
    dbcarhelper.createDatabase();
    dbcarhelper.openDataBase();

    if (s != null) {
        startManagingCursor(ourCursor);
    }

    if (spnr == 1) {

        byMakeModel();
    }

    if (spnr == 2) {

        byBudgetFuel();

    }

    fill_list();

    for (int i = 0; i < arr_make.size(); i++) {
        status.add(false);
    }

    System.out.println("Inside onCreate() ... status all null..! ");

    dialog.dismiss();

}

public void SetId(String s1, String s2, int s) {

    System.out.println("SetMake spnr= |" + spnr + "|");
    System.out.println("SetMake s= |" + s + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

public void SetBudgetFuel(String s1, String s2, int s, int r1, int r2) {

    System.out.println("SetBudget spnr= |" + spnr + "|");
    System.out.println("SetBudget s= |" + s + "|");

    System.out.println("SetBudget r1= |" + r1 + "|");
    System.out.println("SetBudget r2= |" + r2 + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;
    range1 = r1;
    range2 = r2;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

// -------------------------------------------------------------------------

public void byMakeModel() {

    // make n model selected *******************************

    if (str2.equalsIgnoreCase("(Select)")) {

        dbcarhelper.search_by_make(str1);

    } else {

        dbcarhelper.search_by_makemodel(str1, str2);

    }

}

// -------------------------------------------------------------------------

public void byBudgetFuel() {

    // budget or fuel selected
    // ************************************************

    if (str1.equalsIgnoreCase("Any Budget")) {

        dbcarhelper.search_by_fuel(str2);

    }

    else if (str2.equalsIgnoreCase("All")) {

        dbcarhelper.search_by_budget(range1, range2);

    }

    else {

        dbcarhelper.search_by_budgetfuel(str2, range1, range2);

    }

}

// -------------------------------------------------------------------------

public void fill_list() {
    final CarHelper h = new CarHelper(getApplicationContext());

    Toast.makeText(getApplicationContext(), "Inside if case",
            Toast.LENGTH_LONG).show();

    mlist = h.selectAllb2();

    stg1 = new ArrayList<String>();

    int x = 0;
    String stg;

    for (String[] srch : mlist) {

        stg = "Make: " + srch[0] + "\nModel: " + srch[1] + " \nVersion: "
                + srch[2] + "\nPrice: Rs. " + srch[3];

        stg1.add(stg);

        arr_make.add(srch[0]);
        arr_model.add(srch[1]);
        arr_ver.add(srch[2]);

        System.out.println("srch 0" + srch[0] + "\nsrch 1" + srch[1]
                + "\nsrch 2" + srch[2] + "\nsrch 3" + srch[3]);

        x++;
    }

    System.out.println("length--------------------- " + stg1.size());

    // ----------------Image--------------------
    int run = 0;
    c = dbcarhelper.fetchImgRow("1");

    if (c.moveToFirst()) {
        do {

            Image = c.getBlob(c.getColumnIndex("img_str"));

            System.out.println("Image = " + Image);

            System.out.println("run = " + run);

            byte[] decodedString = Base64.decode(Image, Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                    decodedString.length);

            arr_img.add(decodedByte);

            System.gc();

            run++;

        } while (c.moveToNext());

    }
    dbcarhelper.close();

    // ----------------Image--------------------

    System.out.println("run after close() = " + run);

    dataAdapter = new MyCustomAdapter(this, R.layout.new_search_adptr, stg1);

    LV.setAdapter(dataAdapter);

    int[] colors = { 0, 0xff00ffff, 0 };
    LV.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    LV.setDividerHeight(4);

    LV.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            New_Details det = new New_Details();

            System.out.println("Inside Click !");

            String car1 = h.srch_make.get(position);
            String carm1 = h.srch_model.get(position);
            String carv1 = h.srch_ver.get(position);

            det.setImg(arr_img.get(position));

            // Bitmap car_img = arr_img.get(position);
            // System.out.println("position: " + position);
            // System.out.println("arr_img position: " +
            // arr_img.get(position));

            Intent intent = new Intent(Search_list.this, New_Details.class);
            Bundle b = new Bundle();
            b.putString("car1", car1);
            b.putString("carm1", carm1);
            b.putString("carv1", carv1);

            intent.putExtras(b);
            startActivity(intent);

        }
    });

}

private class MyCustomAdapter extends ArrayAdapter<String> {

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);

    }

    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;


        //Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.new_search_adptr, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            //---------- Setting the checkBoxes -----------------------------------

            if (status.get(position)) {

                holder.chkbox.setChecked(true);

            } else {

                holder.chkbox.setChecked(false);
            }

            //-----------------------------------------------------------------

            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);

            convertView.setTag(holder);

            holder.chkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked()) {

                        // User checked (selected) a car
                        // *********************************

                        if (cntr > 1) {

                            // More than two cars selected

                            Toast.makeText(getApplicationContext(),
                                    "Please select only two cars!",
                                    Toast.LENGTH_LONG).show();

                            cb.setChecked(false);

                        } else {

                            // car selected


                            status.set(position, true);
                            cntr++;

                            System.out.println("Chk List Position= " + position );

                            for (int i = 0; i < status.size(); i++) {

                                System.out.println("Chkbox chkd Position= " + i + " Status: "
                                        + status.get(i));
                            }
                        }

                    } else {

                        // User Unchecked (de - selected) a car
                        // *********************************


                        cntr--;
                        status.set(position, false);

                        System.out.println("Unchk List Position= " + position );

                        for (int i = 0; i < status.size(); i++) {

                            System.out.println("UnChkbox chkd Position= " + i + " Status: "
                                    + status.get(i));
                        }

                    }

                }
            });

        } else {

            holder = (ViewHolder) convertView.getTag();
            holder.chkbox.setOnCheckedChangeListener(null);

        }

        holder.text.setText(stg1.get(position).toString());

        // setting image
        holder.imageview.setImageBitmap(arr_img.get(position));

        return convertView;
    }
}

}

Please help me find a solution. Thank you.

Stack Trace

08-17 06:58:16.798: W/dalvikvm(698): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-17 06:58:16.808: E/AndroidRuntime(698): FATAL EXCEPTION: main
08-17 06:58:16.808: E/AndroidRuntime(698): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.MyAuto.Search_list.fill_list(Search_list.java:334)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.MyAuto.Search_list.onCreate(Search_list.java:196)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:123)
08-17 06:58:16.808: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-17 06:58:16.808: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
08-17 06:58:16.808: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:507)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-17 06:58:16.808: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-17 06:58:16.808: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)

回答1:


For reference use the example

Get Selected Item Using Checkbox in Listview

Change your adapter class to below.

You need to override getCount and use SparseBooleanArray

private class MyCustomAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{

    ArrayList<String> myList; 
    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);
        mCheckStates = new SparseBooleanArray(sList.size());
         myList= sList;
    }
    private SparseBooleanArray mCheckStates;
    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }

    @Override
    public int getCount() {
        return stg1.size();
    }

    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.new_search_adptr, null);
            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            convertView.setTag(holder);          

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.chkbox.setTag(position);
        holder.chkbox.setChecked(mCheckStates.get(position, false));
        holder.chkbox.setOnCheckedChangeListener(this);
        holder.text.setText(stg1.get(position).toString());
        holder.imageview.setImageBitmap(arr_img.get(position));
        return convertView;
    }

     public boolean isChecked(int position) {
         return mCheckStates.get(position, false);
     }

     public void setChecked(int position, boolean isChecked) {
         mCheckStates.put(position, isChecked);

     }

     public void toggle(int position) {
         setChecked(position, !isChecked(position));

     }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {


         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

    }
}

To get on Button click

StringBuilder result = new StringBuilder();
for(int i=0;i<youradapter.mCheckStates.size();i++)
{
if(youradapter.mCheckStates.get(i)==true)
{
result.append(arr_make.get(i).toString()+" "+arr_model.get(i).toString()+" "+arr_ver.get(i).toString());
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}



回答2:


In your getView() you're only setting the checked state on newly created views. When views are recycled (convertView != null), the checked state will be that of the old, recycled view.

Move the "Setting the checkBoxes" code part one indentation level up after the if (converView == null) ... else part. That is, change your code from this:

    if (convertView == null) {

        //...
        //create view and init holder

        //---------- Setting the checkBoxes -----------------------------------

        if (status.get(position)) {
            holder.chkbox.setChecked(true);
        } else {
            holder.chkbox.setChecked(false);
        }

        //...

    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.chkbox.setOnCheckedChangeListener(null);
    }

to this:

    if (convertView == null) {

        //...

        //create view and init holder

    } else {
        holder = (ViewHolder) convertView.getTag();
        holder.chkbox.setOnCheckedChangeListener(null);
    }


    //---------- Setting the checkBoxes -----------------------------------

    if (status.get(position)) {
        holder.chkbox.setChecked(true);
    } else {
        holder.chkbox.setChecked(false);
    }



回答3:


I declared the boolean array inside my custom adapter. Also I removed the if statement ie

if (convertView == null)

public class Search_list extends Activity {

static String str1 = "v1", str2 = "v2";
String s, item, idd;
static int spnr, range1, range2;
private CarHelper dbcarhelper = null;
private Cursor ourCursor;
CarHelper h = null;
List<String[]> names2 = null;
ListView LV;
MyCustomAdapter dataAdapter = null;

byte[] Image = null;
Cursor c;
Bitmap decodedByte;

String c1_make = null, c1_model = null, c1_ver = null, c2_make = null,
        c2_model = null, c2_ver = null;

int cntr = 0;

Button b1;

CheckBox cb;

protected ProgressDialog dialog;

List<String[]> mlist = new ArrayList<String[]>();
TextView tv1;

ArrayList<String> stg1;

ArrayList<String> arr_make = new ArrayList<String>();
ArrayList<String> arr_model = new ArrayList<String>();
ArrayList<String> arr_ver = new ArrayList<String>();
ArrayList<Bitmap> arr_img = new ArrayList<Bitmap>();



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_list);

    dialog = ProgressDialog.show(Search_list.this, "Loading",
            "Please Wait...");


    LV = (ListView) findViewById(R.id.list);

    //------------------ Compare button onClick() ------------------------------

    b1 = (Button) findViewById(R.id.btnCmp);
    b1.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {

            int count = 1;

            if (cntr == 1) {
                Toast.makeText(getApplicationContext(),
                        "Please select atleast two cars!",
                        Toast.LENGTH_LONG).show();
            } else {

                for (int i = 0; i < dataAdapter.status.size(); i++) {

                    System.out.println("Compare click Position= " + i + " Status: "
                            + dataAdapter.status.get(i));
                    // System.out.println();

                    if (dataAdapter.status.get(i)) {

                        if (count == 2) {
                            c2_make = arr_make.get(i);
                            c2_model = arr_model.get(i);
                            c2_ver = arr_ver.get(i);

                            count++;
                        }

                        if (count == 1) {
                            c1_make = arr_make.get(i);
                            c1_model = arr_model.get(i);
                            c1_ver = arr_ver.get(i);

                            count++;
                        }

                    } else {

                    }

                }

                Intent intent = new Intent(Search_list.this, DispComp.class);
                Bundle b = new Bundle();

                b.putString("car1", c1_make);
                b.putString("carm1", c1_model);
                b.putString("carv1", c1_ver);

                b.putString("car2", c2_make);
                b.putString("carm2", c2_model);
                b.putString("carv2", c2_ver);

                intent.putExtras(b);
                startActivity(intent);
            }
        }
    });

    //--------------------------------------------------------------------------    

            dbcarhelper = new CarHelper(this);
    dbcarhelper.createDatabase();
    dbcarhelper.openDataBase();

    if (s != null) {
        startManagingCursor(ourCursor);
    }

    if (spnr == 1) {

        byMakeModel();
    }

    if (spnr == 2) {

        byBudgetFuel();

    }

    fill_list();

    System.out.println("Inside onCreate() ... status all null..! ");

    dialog.dismiss();

}

public void SetId(String s1, String s2, int s) {

    System.out.println("SetMake spnr= |" + spnr + "|");
    System.out.println("SetMake s= |" + s + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

public void SetBudgetFuel(String s1, String s2, int s, int r1, int r2) {

    System.out.println("SetBudget spnr= |" + spnr + "|");
    System.out.println("SetBudget s= |" + s + "|");

    System.out.println("SetBudget r1= |" + r1 + "|");
    System.out.println("SetBudget r2= |" + r2 + "|");

    str1 = s1;
    str2 = s2;
    spnr = s;
    range1 = r1;
    range2 = r2;

    System.out.println("SetMake aftr spnr= |" + spnr + "|");

}

// -------------------------------------------------------------------------

public void byMakeModel() {

    // make n model selected *******************************

    if (str2.equalsIgnoreCase("(Select)")) {

        dbcarhelper.search_by_make(str1);

    } else {

        dbcarhelper.search_by_makemodel(str1, str2);

    }

}

// -------------------------------------------------------------------------

public void byBudgetFuel() {

    // budget or fuel selected
    // ************************************************

    if (str1.equalsIgnoreCase("Any Budget")) {

        dbcarhelper.search_by_fuel(str2);

    }

    else if (str2.equalsIgnoreCase("All")) {

        dbcarhelper.search_by_budget(range1, range2);

    }

    else {

        dbcarhelper.search_by_budgetfuel(str2, range1, range2);

    }

}

// -------------------------------------------------------------------------

public void fill_list() {
    final CarHelper h = new CarHelper(getApplicationContext());

    Toast.makeText(getApplicationContext(), "Inside if case",
            Toast.LENGTH_LONG).show();

    mlist = h.selectAllb2();

    stg1 = new ArrayList<String>();

    int x = 0;
    String stg;

    for (String[] srch : mlist) {

        stg = "Make: " + srch[0] + "\nModel: " + srch[1] + " \nVersion: "
                + srch[2] + "\nPrice: Rs. " + srch[3];

        stg1.add(stg);

        arr_make.add(srch[0]);
        arr_model.add(srch[1]);
        arr_ver.add(srch[2]);


        System.out.println("srch 0" + srch[0] + "\nsrch 1" + srch[1]
                + "\nsrch 2" + srch[2] + "\nsrch 3" + srch[3]);

        x++;
    }

    System.out.println("length--------------------- " + stg1.size());

    // ----------------Image--------------------
    int run = 0;
    c = dbcarhelper.fetchImgRow("1");

    if (c.moveToFirst()) {
        do {

            Image = c.getBlob(c.getColumnIndex("img_str"));

            System.out.println("Image = " + Image);

            System.out.println("run = " + run);

            byte[] decodedString = Base64.decode(Image, Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
                    decodedString.length);

            arr_img.add(decodedByte);

            System.gc();

            run++;

        } while (c.moveToNext());

    }
    dbcarhelper.close();

    // ----------------Image--------------------

    System.out.println("run after close() = " + run);

    dataAdapter = new MyCustomAdapter(this, R.layout.new_search_adptr, stg1);

    LV.setAdapter(dataAdapter);

    int[] colors = { 0, 0xff00ffff, 0 };
    LV.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    LV.setDividerHeight(4);

    LV.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            New_Details det = new New_Details();

            System.out.println("Inside Click !");

            String car1 = h.srch_make.get(position);
            String carm1 = h.srch_model.get(position);
            String carv1 = h.srch_ver.get(position);

            det.setImg(arr_img.get(position));


            Intent intent = new Intent(Search_list.this, New_Details.class);
            Bundle b = new Bundle();
            b.putString("car1", car1);
            b.putString("carm1", carm1);
            b.putString("carv1", carv1);

            intent.putExtras(b);
            startActivity(intent);

        }
    });

}

private class MyCustomAdapter extends ArrayAdapter<String> {

     private ArrayList<Boolean> status = new ArrayList<Boolean>();

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<String> sList) {
        super(context, textViewResourceId, sList);

        for (int i = 0; i < arr_make.size(); i++) {
            status.add(false);
        }

        System.out.println("Inside My Custom Adapter.. status all null..! ");

    }

    private class ViewHolder {
        TextView text;
        CheckBox chkbox;
        ImageView imageview;
    }


     @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arr_make.size();
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {

        ViewHolder holder = null;


        //Log.v("ConvertView", String.valueOf(position));

        //if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.new_search_adptr, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView
                    .findViewById(R.id.adapterText1);
            holder.chkbox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            //---------- Setting the checkBoxes ------------------------

            if (status.get(position)) {

                holder.chkbox.setChecked(true);

            } else {

                holder.chkbox.setChecked(false);
            }

            //----------------------------------------------------------

            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.imageView1);

            convertView.setTag(holder);

            holder.chkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked()) {

                        // User checked (selected) a car
                        // *********************************

                        if (cntr > 1) {

                            // More than two cars selected

                            Toast.makeText(getApplicationContext(),
                                    "Please select only two cars!",
                                    Toast.LENGTH_LONG).show();

                            cb.setChecked(false);

                        } else {

                            // car selected

                            status.set(position, true);
                            cntr++;

                            System.out.println("Chk List Position= " + position );

                            for (int i = 0; i < status.size(); i++) {

                                System.out.println("Chkbox chkd Position= " + i + " Status: "
                                        + status.get(i));
                            }
                        }

                    } else {

                        // User Unchecked (de - selected) a car
                        // *********************************


                        cntr--;
                        status.set(position, false);

                        System.out.println("Unchk List Position= " + position );

                        for (int i = 0; i < status.size(); i++) {

                            System.out.println("UnChkbox chkd Position= " + i + " Status: "
                                    + status.get(i));
                        }

                    }

                }
            });


        holder.text.setText(stg1.get(position).toString());

        // setting image
        holder.imageview.setImageBitmap(arr_img.get(position));

        return convertView;
    }
}

}


来源:https://stackoverflow.com/questions/18180349/list-view-custom-adapter-randomly-checking-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!