Issue
I have a GriView which is populated with items and on clicking the item on the gridview I change the background resource to an image I have in res folder. My problem is when scrolling and going back to the selected item, it no longer has the background resource. Instead some other item of the gridview gets the background.
In my onCreate method I have the following code to populate my GridView:
EDIT Based on the suggestions I got I created a custom adapter and populated the gridview. The problem I have now is, none pf my gridView elements are getting populated now. When I set up my adapter I pass a boolean array and a data array. The data array contains all the data that should be used to populate the GridView and the boolean array is to find out if the grid item has been selected.
EDIT: The GridView elements that have been selected are now highlighted even after scrolling, but now other elements that never were selected seem to be getting highlighted.
Here is the adapter class:
public class HoursAdapter extends BaseAdapter {
private Context mContext;
private boolean isSelected[];
private String hours[];
public HoursAdapter(Context c, boolean selected[], String hours[]){
this.isSelected = selected;
this.mContext = c;
this.hours = hours;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return hours.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if(convertView==null){
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.hourview_item, null);
holder = new ViewHolder();
holder.hours =(TextView) convertView.findViewById(R.id.hours_text);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
if(isSelected[arg0-1])
holder.hours.setBackgroundResource(R.drawable.item_background_focused);
holder.hours.setText(hours[arg0-1]);
return convertView;
}
static class ViewHolder{
TextView hours;
}
}
And this my code for setting up the adapter:
HoursAdapter adapter = new HoursAdapter(this,isSelected,data);
hoursView.setAdapter(adapter);
hoursView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
v.setBackgroundResource(R.drawable.item_background_focused);
}
});
I would greatly appreciate any help!
Solution
Seems like you're not considering how view recycling works within adapters. Using an adapter views get recycled during scrolling, that is the same view used to render the item you just clicked on, should be re-used to render another item after a scroll operation.
You should define your own adapter class and manage the selected/unselected status yourself (eg: using an array of booleans with the same size of your data) and set/reset background properly via BaseAdapter.getView(int, View, ViewGroup)
. This a pretty basic topic, and you will get plenty of tutorials and reference on Official Android Developers Portal
Answered By - a.bertucci
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.