I have this code in my getView of BaseAdapter class:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.listview_item, parent, false);
} else {
v = convertView;
}
LinearLayout mLayout = v.findViewById(R.id.layout_list_item);
if (!mList.get(position).isOperative()) {
mLayout.setBackgroundResource(R.drawable.list_item_background_inactive);
} else {
mLayout.setBackgroundResource(R.drawable.list_item_background_active);
}
TextView mTextOne = v.findViewById(R.id.text_one);
TextView mTextTwo = v.findViewById(R.id.text_two);
mTextOne.setText(mList.get(position).getPropertyOne());
mTextTwo.setText(mList.get(position).getPropertyTwo());
return v;
}
In
if (!mList.get(position).isOperative())
I want to do a background setting and the background was applied properly at the begin, but if I scroll, I'm losing the initial setting.
in mList there are objects with 3 properties (String propertyOne, propertyTwo, Boolean isOperative).
Any ideas?
Thanks in advance!
I find the solution by using ViewHolder:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
holder.layoutOperative = convertView.findViewById(R.id.layout_list_item);
holder.txtOne = convertView.findViewById(R.id.text_one);
holder.txtTwo = convertView.findViewById(R.id.text_two);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (mBuildingsList.get(position).isOperative()) {
holder.layoutOperative.setBackgroundResource(R.drawable.list_item_background_active);
} else {
holder.layoutOperative.setBackgroundResource(R.drawable.list_item_background_inactive);
}
holder.txtOne.setText(mList.get(position).getPropertyOne());
holder.txtTwo.setText(mList.get(position).getPropertyTwo());
return convertView;
}
Thanks to all!