I have a listview for my menu with a baseadapter.
My Activity :
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<?> arg0, View view, int position, long arg3)
{
view.setSelected(true);
}
});
listView_menu.setSelection(0);
My getView method of BaseAdapter_Menu (extends BaseAdapter) :
@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);
}
Each item has a selector for its background with 2 differents background depending of its state (SELECTED or NOT).
When I click on an item in my listview, I set to my view item.setSelected("true").
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.
I found the solution ! 🙂
In my onCreate I appy this code :
In my BaseAdater_Menu :
I create
private int PositionSelected = 0;I add this method
And I override the getView method
Thank you for your help 🙂