There is the following adapter:
private class RepeatingAdapter extends ArrayAdapter<Repeatable> {
private List<Repeatable> items;
private LayoutInflater inflater;
private int resource;
public RepeatingAdapter(Context context, int resource,
List<Repeatable> items) {
super(context, resource, items);
this.items=items;
this.resource=resource;
inflater=LayoutInflater.from(context);
}
@Override
public View getView(int position, View view, ViewGroup group) {
View item=(view==null) ? inflater.inflate(resource, null) : view;
TextView title=(TextView)item.findViewById(R.id.listItemRepeatingTypeTitle);
title.setText(items.get(position).getTitle());
items.get(position).setCommand(new RedRectangleCommand(item));
Log.e("view", item.toString());
return item;
}
@Override
public Repeatable getItem(int position) {
return items.get(position);
}
}
Please, note, that we create new RedRectangleCommand and send View created to it. So, we also do the following thing:
repeatingList.setAdapter(new RepeatingAdapter(this,
R.layout.list_item_repeating_type, types));
repeatingList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
((Repeatable)parent.getItemAtPosition(position)).mark();
}
});
Please, note, we execute mark() method for selected item (Mark method for Repeatable executes mark() method for command). All is good. And the last code for command:
private class RedRectangleCommand extends Command {
private View view;
public RedRectangleCommand(View view) {
this.view=view;
}
@Override
public void mark() {
ImageView image=(ImageView)view.findViewById(R.id.listItemRepeatingTypeImage);
image.setBackgroundColor(Color.RED);
image.invalidate();
}
@Override
public void unmark() {
ImageView image=(ImageView)view.findViewById(R.id.listItemRepeatingTypeImage);
image.setBackgroundColor(Color.BLACK);
}
}
I need to change color of ImageView from selected View by click. But it doesn’t work! Also, my log shows me that selected item and item from Command are different ones. What’s going on?
Maybe you can try this instead and see if it’s any different. What it does is try to refer to the RedRectangleCommand object directly by making it a method of your custom adapter:
Then in your adapter:
Also i think a real issue is that part in your getView:
Because as of now, it is making a new one of these objects, re-instantiating, over and over again no-matter if it was used or not, only as long as the listView row disappeared from view and has come back again. You can imagine how wasteful and troublesome it might be especially if trying to refer to a specific instance. there should be several ways around this. For instance, you could only do a setCommand if that Repeater doesn’t have one yet. Maybe make a boolean method to check is it has one of those RedRectangleCommand or something like checking if convertView is null. Another idea i’m trying to make solid is doing some constructor work but needing a parameter of
Viewseems troublesome: