Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView with multiple select and custom adapter
    primarykey
    data
    text
    <p>I have a <code>ListView</code> with a custom adapter. The <code>ListView</code> allows multiple select, but somehow it's not recognising when an item is selected.</p> <p>I've already made the adapter items extend Checkable, but still the <code>getCheckedItemPositions()</code>returns an array of falses.</p> <p>I guess there's something fundamental I'm doing wrong, but I have been unable so far to find examples of multiple select ListViews where the adapter was not an ArrayAdapter using the default layout for multiple selects.</p> <p>Any help would be much appreciated.</p> <p>Code is below:</p> <p>Main class:</p> <pre><code>listView = (ListView) findViewById(R.id.cardlist); tca = new TextCardAdapter(mInflater); listView.setAdapter(tca); </code></pre> <p>Adapter:</p> <pre><code>public class TextCardAdapter extends BaseAdapter { private int count = 0; private List&lt;CheckableCard&gt; cardList = new ArrayList&lt;CheckableCard&gt;(); private LayoutInflater mInflater; public TextCardAdapter(LayoutInflater inflater) { this.mInflater = inflater; } @Override public int getCount() { return count; } @Override public Object getItem(int position) { return cardList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.textlayout_row, null, false); convertView.setClickable(true); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.card_name); holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final CheckableCard card = cardList.get(position); holder.text.setText(card.card.toString()); holder.checkbox.setChecked(card.isChecked()); holder.checkbox.setOnClickListener(card.checkListener); convertView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { card.checkListener.onClick(v); holder.checkbox.setChecked(card.isChecked()); } }); return convertView; } public void add(Card card) { cardList.add(new CheckableCard(card)); count++; notifyDataSetChanged(); } public void addAll(Collection&lt;Card&gt; cardColl) { for (Card c : cardColl) add(c); } public void removeAll() { count = 0; cardList.clear(); notifyDataSetChanged(); } public Card getCard(int position) { CheckableCard cc = (CheckableCard) getItem(position); if (cc == null) return null; return cc.card; } public class CheckableCard implements Checkable { private boolean checked = false; public final Card card; public final OnClickListener checkListener; public CheckableCard(Card card) { this.card = card; checkListener = new OnClickListener() { @Override public void onClick(View v) { toggle(); } }; } @Override public boolean isChecked() { return checked; } @Override public void setChecked(boolean checked) { this.checked = checked; } @Override public void toggle() { checked = !checked; } } static class ViewHolder { TextView text; CheckBox checkbox; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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