I create a gridview with some String. WHen I click on each of it I change its status to activated and I use that property to change the color of the selected item.
All works correctly if I don't scroll the gridview, but If I selected one item and after I scroll the gridview , the item start to change its color every time that I scroll the gridview.
Can you help me to find the error ? :(
THis is my code :
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.gridview_command_element, null);
viewHolder = new ViewHolder();
viewHolder.boh = (TextView) view.findViewById(R.id.cmd_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
final String Name = lista.get(i);
if (Name != null)
{
viewHolder.boh.setText(Name);
}
return view;
}
static class ViewHolder
{
TextView boh;
}
this is the onitemclicklistener:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
if(adapter4selectedCmd.getCount() < 10)
{
view.setActivated(true);
String cmd = (String)adapter.getItem(position);
}
else
{
view.setActivated(false);
}
}
This is the xml layout and selector :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="@color/tableTextSelectedColor" />
<item android:color="@color/tableTextColor" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:id="@+id/ecu_list_element"
>
<TextView android:id="@+id/cmd_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:textColor="@color/text_selector"
android:gravity="center_horizontal|center_vertical"/>
</LinearLayout>