jozencl jozencl - 3 months ago 21
Android Question

I am trying to use the getView method in my adapter for a gridview and its not working?

below is my adapter class

public class ImageAdapter extends BaseAdapter {
private Context context;
public static ProfileData[]profileData={
new ProfileData(R.drawable.profileone,"profile1"),
new ProfileData(R.drawable.profiletwo,"profile2"),
new ProfileData(R.drawable.profilethree,"profile3"),
new ProfileData(R.drawable.profilefour,"profile4"),
new ProfileData(R.drawable.profilefive,"profile5"),
};



public ImageAdapter(Context context){
this.context=context;

}

@Override
public int getCount() {
return profileData.length;
}

@Override
public Object getItem(int position) {
return profileData[position];
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ImageView imageView=new ImageView(context);
imageView.setImageResource(profileData[position].getDrawable());
return imageView;
}
}


As you can tell i created an adapter to set an imageview imageresource to a profile. However, I am implementing the itemonclick method in a fragment, and i am trying to get the imageview at a particular position but its not working, can someone please tell me why?
Here is my fragment where I am trying to get the imageview

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_welcome_fourth,container,false);
adapter=new ImageAdapter(getContext());

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

ImageView image=(ImageView) adapter.getView(position,view,null);
image.setColorFilter(Color.BLUE);

}
});
return view;

}

Answer

That's because getView() is only meant to be used by the view for filling in the list items, and nothing else.

What you need to do is have some sort of variable in your adapter that holds the state of your list item. I assume since you are highlighting the entire list item when clicked, that is a "selected" type of state.

So for example you could have this in the adapter:

    private boolean[] selected = new boolean[profileData.length];

(This is a simple way, not the best way.)

Also a public setter:

    public void setSelected(int position, boolean sel) {
        selected[position] = sel;
        notifyDataSetChanged();
    }

In getView, you use this flag to set your color:

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ImageView imageView=new ImageView(context);
        imageView.setImageResource(profileData[position].getDrawable());
        if (selected[position]) {
            imageView.setColorFilter(Color.BLUE);
        } else {
            imageView.setColorFilter(null);
        }
        return imageView;
    }

And in the event handler, you update the model and call notifyDataSetChanged():

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            ImageAdapter adapter = (ImageAdapter) adapterView.getAdapter();
            adapter.setSelected(position, true);
        }
    });

So getView always reads the model, event handlers always update the model. If you ever find yourself trying to change a view from within an event handler, you know you're doing something wrong.

Comments