Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Take a look at <a href="http://nickcharlton.net/post/building-custom-android-listviews">this link</a></p> <p>This is the resource I ve read to implement my own custom list view. It was really helpful for me. I think you need to add a checkbox in the list_item.xml file and change the layout attributes. </p> <p>You should also need to add a listener to the CheckBox object onCheckedChanged inside custom adapter. </p> <p>Here is a sample code of mine for a custom adapter;</p> <pre><code>public class EntryAdapter extends ArrayAdapter&lt;Entry&gt; { private Activity context; private LayoutInflater inflater; private ArrayList&lt;Entry&gt; entries; public EntryAdapter(Activity context, ArrayList&lt;Entry&gt; objects) { super(context, R.layout.entry_item, objects); // TODO Auto-generated constructor stub this.context = context; this.entries = objects; } public View getView (int position, View convertView, ViewGroup parent) { View viewRow = convertView; if(viewRow == null) { inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); viewRow = inflater.inflate(R.layout.entry_item , null, true); } Entry e = entries.get(position); if(e != null) { TextView aka = (TextView) viewRow.findViewById(R.id.authorTextView); TextView content = (TextView) viewRow.findViewById(R.id.entryContentTextView); if(aka != null &amp;&amp; content != null){ aka.setText(e.getAka()); content.setText(e.getContent()); } } return viewRow; } </code></pre> <p>}</p> <p>In this code, there is no event listeners. What you should firstly do is to add is define an arraylist of boolean like </p> <pre><code>ArrayList&lt;boolean&gt; selectedItems; </code></pre> <p>after that, inside the getView method you should add the following;</p> <pre><code>CheckBox cb = (CheckBox) findViewById(R.id.checkBoxId); </code></pre> <p>after that, you should add onCheckStateChanged listener inside the getView() method</p> <pre><code>cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if ( isChecked ) { // find out the item is selected or not and add to selected arraylist by using the position of the element } } }); </code></pre> <p>and lastly, you should have getSelectedMethod that returns the arraylist of selected items with respect to their positions.</p> <p>Hope I understand the question and it will be helpful for you.</p>
 

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