Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select in code the first item in a listview (with baseadapter) to change its background
    text
    copied!<p>I have a listview for my menu with a baseadapter.</p> <p>My Activity :</p> <pre><code>listView_menu = (ListView) findViewById(R.id.listView_menu); model_category = new Model_Category(context); listView_menu.setAdapter(new BaseAdapter_Menu(context, model_category.GetAllDifferentCategory())); listView_menu.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int position, long arg3) { view.setSelected(true); } }); listView_menu.setSelection(0); </code></pre> <p>My getView method of BaseAdapter_Menu (extends BaseAdapter) :</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; if (rowView == null) { rowView = this.inflater.inflate(R.layout.customitemlistview_menu, null); ViewHolder viewHolder = new ViewHolder(); viewHolder.category = (TextView) rowView.findViewById(R.id.category_menu); rowView.setTag(viewHolder); } ViewHolder holder = (ViewHolder)rowView.getTag(); holder.category.setText(this.data.get(position)); return (rowView); } </code></pre> <p>Each item has a selector for its background with 2 differents background depending of its state (SELECTED or NOT).</p> <p>When I click on an item in my listview, I set to my view <code>item.setSelected("true")</code>. </p> <p>So when I click, the item's background changes, but how to change the background of the first item of my listview without clicking on it. I have already tried 'listView_menu.setSelection(0)' but it doesn't work.</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