I've a listview in android app in which some of the listview items are to be displayed differently based on bound object properties. I'm trying to do in adapter itself when binding takes place but the result is not as expected.
As per the conditions only Mr C's item buttons should be greyed out but along with that couple other go grey too. Can someone please provide some insight in this behavior?
MainActivity:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.relatedFeelingsList);
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("Ford",2008, "Mr A"));
cars.add(new Car("BMW",2009, "Mr B"));
cars.add(new Car("Mercedes",1999, "Mr C"));
cars.add(new Car("Honda",2004, "Mr D"));
cars.add(new Car("Maruti",2005, "Mr E"));
cars.add(new Car("Ferrari",2015, "Mr F"));
ArrayAdapter<Car> adapter = new ListAdpater(getApplicationContext(),R.id.relatedFeelingsList,cars);
listView.setAdapter(adapter);
Adapter Code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Car car = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_item, null);
holder = new ViewHolder();
holder.carNameView = (TextView) convertView.findViewById(R.id.name);
holder.carOwnerView = (TextView) convertView.findViewById(R.id.owner);
holder.startButton = (Button) convertView.findViewById(R.id.btnStart);
holder.parkButton = (Button) convertView.findViewById(R.id.btnPark);
holder.reportButton = (Button) convertView.findViewById(R.id.btnReport);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.carNameView.setText(car.getMake());
holder.carOwnerView.setText(car.getOwner());
setCarViewInListView(convertView, holder, car);
return convertView;
}
private void setCarViewInListView(View convertView, ViewHolder holder, Car car) {
if (car.getYear() < 2000) {
setOldCarView(convertView, holder, car);
}
}
private void setOldCarView(View convertView, ViewHolder holder, Car car) {
// int color = getContext().getResources().getColor(R.color.greyColor);
DisableButton(holder.startButton);//.setBackgroundColor(color);
DisableButton(holder.parkButton);//.setBackgroundColor(color);
DisableButton(holder.reportButton);//.setBackgroundColor(color);
}
private void DisableButton(Button button) {
button.setEnabled(false);
button.setClickable(false);
button.setBackgroundColor(Color.GRAY);
}
ListItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
>
<RelativeLayout
android:id="@+id/feelingDetails"
android:layout_weight="1"
android:layout_width="match_parent"
android:padding="7dp"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/feelingTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="Car"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/owner"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Owner" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="7dp">
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="30dp"
style="@style/buttonStyle"
android:paddingRight="7dp"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="@+id/btnPark"
android:layout_marginLeft="2dp"
android:paddingRight="7dp"
android:layout_width="wrap_content"
android:layout_height="30dp"
style="@style/buttonStyle"
android:layout_weight="1"
android:text="Park" />
<Button
style="@style/buttonStyle"
android:id="@+id/btnReport"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_weight="1"
android:paddingRight="7dp"
android:layout_alignTop="@id/btnPark"
android:layout_marginLeft="2dp"
android:text="Report" />
</LinearLayout>
</LinearLayout>
But the result I get is as below : listview out put
1 Answer 1
Views get recycled, this means that after you've greyed out a List element, it might get re-used somewhere else and still be greyed out.
You have to undo this and restore the original View state, e.g. by adding this here:
if (car.getYear() < 2000) {
setOldCarView(convertView, holder, car);
} else {
setNewCarView(convertView, holder, car);
}