I created a ListView with a custom array adapter of MyObject. One instance of MyObject contains a String and a boolean. When I visualize the ListView all my items appear in bold and if you tap on one of those items you start a new Activity. What that I want to do is that every time that you press back and come back to that ListView, you visualize the item that you clicked not bold. This is my custom ArrayAdapter:
public class MyAdapter extends ArrayAdapter<MyObject> {
private final Context context;
private final ArrayList<MyObject> values;
public MyAdapter(Context context, ArrayList<MyObject> values) {
super(context, R.layout.my_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = inflater.inflate(R.layout.my_item, parent, false);
}
TextView textView = (TextView) convertView.findViewById(R.id.itemName);
textView.setText(values.get(position).getMyObjectName());
if(values.get(position).isSetted()){
textView.setTypeface(null, 0);
}
return convertView;
}
}
In my Activity I put:
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
and the code actually works, But there is a problem: after you tapped some items, other untapped items become not bold when you come back! This happens in a random way! Why is this happening?
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = objList.get(position).getMyObjectName();
objList.get(position).setSetted(true);
to_ans.putExtra("ItemName", item);
startActivity(to_ans);
}
The boolean value of the items initially are all set at false.
You’ll want to revert to the default bold style if
isSetted()isfalse(to revert to the default style a potential recycledView(for the case whenconvertViewis notnull) who may be set without bold):