Reputation: 1276
I have a normal setup for a ListView
and Custom ArrayAdapter
.
and i'm stipulating if something equals to something then change color of that item.
at the first load, the condition works for correct items that i change thier color (if condition).
The issue begins to appear when i scroll the ListView
up and down continually, the color changing for wrong items randomly and reverts back to original as i keep scrolling. even the correct ones randomly changing. till i get all the items on the list set with that color!
Let's say only one item has (true) value and the rest is (false) then i condition if true, change color. but when i scroll (not at first load) other items gets that color even they're false.
but the data i set with disabledTextView.setText("correct item");
does not change it keeps as it's correct, which is good.
MyCustomAdapter.java
public class MyCustomAdapter extends ArrayAdapter<HashMap<String, String>> {
private Context context;
private ArrayList<HashMap<String, String>> myArray;
public MyCustomAdapter(Context context, ArrayList<HashMap<String, String>> theArray) {
super(context, R.layout.my_single_list_item, theArray);
this.context = context;
this.myArray = theArray;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_single_list_item, parent, false);
}
HashMap<String, String> arrayItem = myArray.get(position);
boolean disabled = Boolean.valueOf(arrayItem.get("disabled"));
TextView disabledTextView = convertView.findViewById(R.id.disabledTextView);
disabledTextView.setText(String.valueOf(disabled));
if(disabled) {
disabledTextView.setTextColor(getContext().getResources().getColor(R.color.design_default_color_error));
}
return convertView;
}
}
my_single_list_item.xml
<?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="match_parent">
<TextView
android:id="@+id/disabledTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="@android:color/black" />
</LinearLayout>
What causes this problem?
Update (Solved) : but more information still needed!
after getting rid of using convertView
and replaced it with customView
with new LayoutInflater
:
public View getView(int position, View convertView, ViewGroup parent) {
View customView = LayoutInflater.from(getContext()).inflate(R.layout.my_single_list_item, parent, false);
}
TextView hotspotUserDisabled = customView.findViewById(R.id.hotspotUserDisabled);
//.. etc
The problem is solved. i'm still too confused with that, should i only use new View
inflate only if View convertView
is null?
because the IDE shows a hint :
When implementing a view Adapter, you should avoid unconditionally inflating a new layout; if an available item is passed in for reuse, you should try to use that one instead. This helps make for example ListView scrolling much smoother.
What if i need to fix this and still use the View
that is passed to keep scrolling smoother?
Thanks!
Upvotes: 0
Views: 76
Reputation: 1875
You need to define else case in getView method
if(disabled) {
disabledTextView.setTextColor(getContext().
getResources().getColor(R.color.design_default_color_error));
}else{
// add code here
}
Upvotes: 1