Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>IMHO, it's easier to use a custom adapter:</p> <pre><code>class CustomAdapter extends ArrayAdapter&lt;CustomRowItem&gt; { Context context; public CustomAdapter(Context context, int resourceId, List&lt;CustomRowItem&gt; items) { super(context, resourceId, items); this.context = context; } private class ViewHolder { TextView txt; View layout; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; CustomRowItem rowItem = getItem(position); LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = mInflater.inflate(R.layout.сustom_list, null); holder = new ViewHolder(); holder.txt = (TextView) convertView.findViewById(R.id.сustom_list_txt); holder.layout = convertView.findViewById(R.id.сustom_list_layout); convertView.setTag(holder); } else holder = (ViewHolder) convertView.getTag(); holder.txt.setText(rowItem.getText()); if(rowItem.isChecked()) holder.layout.setBackgroundColor(-16720999); //color for checked else holder.layout.setBackgroundColor(0); //color for unchecked return convertView; } } class CustomRowItem { private boolean value; private String text; public CustomRowItem(String text, boolean value) { this.text = text; this.value = value; } public boolean isChecked() { return value; } public void setChecked(boolean checked) { value = checked; } public String getText() { return text; } void setText(String text) { this.text = text; } } </code></pre> <p>сustom_list.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:id="@+id/сustom_list_layout" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/сustom_list_txt" android:textSize="20sp" android:layout_width="fill_parent" android:layout_height="fill_parent"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>How use:</p> <pre><code>public class ExampleAct extends Activity { final List&lt;CustomRowItem&gt; list = new ArrayList&lt;CustomRowItem&gt;(); CustomAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.example); ListView listView=(ListView)findViewById(R.id.listView); adapter = new CustomAdapter(this, R.layout.сustom_list, list); listView.setAdapter(adapter); list.add(new CustomRowItem("unchecked item",false)); list.add(new CustomRowItem("checked item",true)); adapter.notifyDataSetChanged(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //unchecked on click @Override public void onItemClick(AdapterView&lt;?&gt; parent, View itemClicked, int index, long id) { if(list.get(index).isChecked()) { list.get(index).setChecked(false); //uncheck adapter.notifyDataSetChanged(); } else { // other actions } }); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { //checked on long click @Override public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View itemClicked, int index, long id) { list.get(index).setChecked(true); //check adapter.notifyDataSetChanged(); return true; // or false for calling context menu } }); } </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