Note that there are some explanatory texts on larger screens.

plurals
  1. POList View custom adapter randomly checking values
    text
    copied!<p>I have made a list view with a custom Adapter having an imageview, textview and a checkBox</p> <pre><code>--------------------------------------- &lt;ImageView --- textView --- checkbox &gt; --------------------------------------- </code></pre> <p>The listview has an onClickListener to it and the checkboxes have onCheckListener..</p> <p>I have used a boolean array (named as status) to set the checkboxes checked or not initialized to false in the begining. </p> <p>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.</p> <p>Also many other checkboxes are automatically checked.</p> <p><strong>Java Code</strong></p> <pre><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&lt;String[]&gt; names2 = null; ListView LV; MyCustomAdapter dataAdapter = null; private ArrayList&lt;Boolean&gt; 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&lt;String[]&gt; mlist = new ArrayList&lt;String[]&gt;(); TextView tv1; ArrayList&lt;String&gt; stg1; ArrayList&lt;String&gt; arr_make = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; arr_model = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; arr_ver = new ArrayList&lt;String&gt;(); ArrayList&lt;Bitmap&gt; arr_img = new ArrayList&lt;Bitmap&gt;(); @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&lt;Boolean&gt;(); / 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 &lt; 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 &lt; 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&lt;String&gt;(); 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&lt;?&gt; 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&lt;String&gt; { public MyCustomAdapter(Context context, int textViewResourceId, ArrayList&lt;String&gt; 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 &gt; 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 &lt; 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 &lt; 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; } } } </code></pre> <p>Please help me find a solution. Thank you.</p> <p><strong>Stack Trace</strong></p> <pre><code>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) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload