Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I get something... Instead of <code>Chk.setOnCheckedChangeListener()</code> I've changed to <code>Chk.setOnClickListener(new OnClickListener()</code></p> <pre><code>public class SPCMjereAdapter extends BaseAdapter { private Context context; public SPCMjereAdapter(Context c) { context = c; } public int getCount() { return MyArrList.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = inflater.inflate(R.layout.activity_list_row, null); } TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); txtOpis.setText(MyArrList.get(position).get("OpisMjere") ); TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); if(yourSelectedItems.contains((position))){ Chk.setChecked(true); }else{ Chk.setChecked(false); } Chk.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (Chk.isChecked()) { yourSelectedItems.add(position); } else { if(yourSelectedItems.contains(position)){ yourSelectedItems.remove((position)); } } } }); return convertView; } } </code></pre> <p>Now it works fine when scrolling up/down. But there is another problem and i'm little bit confused. When scrolling and randomly check/uncheck items program crashes and these errors I get:</p> <pre><code>FATAL EXCEPTION: main java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) at java.util.ArrayList.remove(ArrayList.java:399) at com.dem.spckontrola.SPCKontrola$SPCMjereAdapter$1.onClick(SPCKontrola.java:830) at android.view.View.performClick(View.java:4084) at android.widget.CompoundButton.performClick(CompoundButton.java:100) at android.view.View$PerformClick.run(View.java:16966) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method </code></pre> <p>)</p> <p><strong>Ok. I solved this by adding</strong> <code>yourSelectedItems.remove(yourSelectedItems.indexOf(position));</code></p> <p>So now the correct solution is:</p> <pre><code> public class SPCMjereAdapter extends BaseAdapter { private Context context; public SPCMjereAdapter(Context c) { context = c; } public int getCount() { return MyArrList.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = inflater.inflate(R.layout.activity_list_row, null); } TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); txtOpis.setText(MyArrList.get(position).get("OpisMjere") ); TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); if(yourSelectedItems.contains((position))){ Chk.setChecked(true); }else{ Chk.setChecked(false); } Chk.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (Chk.isChecked()) { yourSelectedItems.add(position); } else { if(yourSelectedItems.contains(position)){ yourSelectedItems.remove(yourSelectedItems.indexOf(position)); } } } }); return convertView; } } </code></pre> <p>But now I'm facing with new problem: How to get list of all checked and unchecked items where some checkboxes (items) are not visible due to many items in listview which are visible only when scrolling down?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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