2

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)
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Carbon
  • 133
  • 1
  • 4
  • 21
  • looking for this http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177 ? – Raghunandan Aug 12 '13 at 05:54
  • also check the discussion and solution by Romain Guy @ https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M – Raghunandan Aug 12 '13 at 05:59
  • yes some what same, but wrong position of the list is being fired.. may be because android recycles the view – Carbon Aug 12 '13 at 05:59
  • Should I also make a separate adapter for checkBoxes? – Carbon Aug 12 '13 at 06:02
  • see here example related to this http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html or http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html or http://dj-android.blogspot.in/2012/04/milti-selection-listview-android-with.html – Khan Aug 12 '13 at 06:03
  • no need for a seperate adapter – Raghunandan Aug 12 '13 at 06:07
  • @Pooja check my answer and give it a try – Raghunandan Aug 12 '13 at 06:11
  • 1
    You are using status of outer-class, in my case when i used variable of outer class as u did, was causing defect same as you, just use it in your inner custom adapter class, and pass array from your activity to adapter by constructor param – Yahya Arshad Aug 12 '13 at 06:13
  • You need to use setTag() and getTag() for every element of your listview. – arshu Aug 12 '13 at 06:18
  • @Pooja http://chat.stackoverflow.com/rooms/35256/discussion-between-raghunandan-and-pooja. chat here – Raghunandan Aug 17 '13 at 07:14

3 Answers3

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();
}
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • How do I get the checkboxes which have been checked, I need to get this on a button click – Carbon Aug 12 '13 at 06:36
  • which arraylist data you want to get? I mean you have used arraylist to set the text in listview rows. Is it stg1? – Raghunandan Aug 12 '13 at 06:38
  • No three arrayLists.. arr_make, arr_model and arr_ver – Carbon Aug 12 '13 at 06:39
  • i don't see you using 3 arraylists. i see this `holder.text.setText(stg1.get(position).toString());` arraylist stg1 – Raghunandan Aug 12 '13 at 06:39
  • I am using it just to display it on the list in textView. And I hav made 3 different ArrayLists for storing and passing the values to another intent on click of a buttton – Carbon Aug 12 '13 at 06:42
  • @Pooja i don't know what your doing you have 3 arraylists but you don't use it to display in the listview. Try the above it on button click – Raghunandan Aug 12 '13 at 06:47
  • Its printing out only the first value ie only one value is being stored in the result string.. When I unchecked the first value and selected a different one the toast was empty – Carbon Aug 12 '13 at 07:05
  • @Pooja it will work if you do it right. i am confused the way you are using lists. More info check this https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M . I used romain guys solution and it works for me – Raghunandan Aug 12 '13 at 07:07
  • @Pooja instead of this `youradapter.mCheckStates.size()` use `arr_make.size()` in for loop. i think that should work for you. – Raghunandan Aug 12 '13 at 07:12
  • Thank you for your time, support and patience... means a lot! – Carbon Aug 12 '13 at 08:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35256/discussion-between-raghunandan-and-pooja) – Raghunandan Aug 12 '13 at 08:55
  • because it is crashing when the list length is above 110 – Carbon Aug 17 '13 at 07:06
  • @Pooja if it crashes you are doing it wrong does not mean my code is wrong. Its a mistake. post the stack trace. and make sure use `arr_make.size()` instead of `youradapter.mCheckStates.size()` in for loop. its just the size that you have to get right – Raghunandan Aug 17 '13 at 07:07
  • @Pooja chat here http://chat.stackoverflow.com/rooms/35256/discussion-between-raghunandan-and-pooja. it OOM meaning your app ran out of memory – Raghunandan Aug 17 '13 at 07:16
1

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);
    }
laalto
  • 150,114
  • 66
  • 286
  • 303
0

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;
    }
}

}
Carbon
  • 133
  • 1
  • 4
  • 21
  • 1
    why did you remove `converView==null`. oonly when your view is null you need to `findViewById`. do it for large list view items you will see the performance issues. i don't think what you are doing is right – Raghunandan Aug 12 '13 at 09:01
  • yes you are right, but I wanted a quick fix to meet my deadline today.. I will still apply the other way later. – Carbon Aug 12 '13 at 11:31