问题
- What i am doing:: I have a
gridview
where i am displaying images I adapter of thegridview
has image and acheckbox
- What is happening:: When i check the checkbox and i scroll the
gridview
down & when iscrollback
, the selectedcheckbox
is unselected - Question:: How can i prevent this What changes should i need to make in the code
grid_view_image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:background="#000000"
android:padding="1dp" />
<CheckBox
android:id="@+id/ch_bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="CheckBox" />
</RelativeLayout>
AdapterGridViewImage.java
public class AdapterGridViewImage extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();
public AdapterGridViewImage(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
//imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView flag;
CheckBox ch_bx;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
resultp = data.get(position);
flag = (ImageView) itemView.findViewById(R.id.flag);
ch_bx=(CheckBox) itemView.findViewById(R.id.ch_bx);
//Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);
Picasso.with(context)
.load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
.resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(flag);
return itemView;
}
}
回答1:
Please modify your code.
1) We have to use ViewHolder class for encapsulation.
2) We have to set tag values of checkbox so when view recreate at that time we can take values from tag.
public AdapterGridViewImage(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
//imageLoader = new ImageLoader(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if(convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.grid_view_image_item, parent, false);
holder.flag = (ImageView) convertView.findViewById(R.id.flag);
holder.ch_bx=(CheckBox) convertView.findViewById(R.id.ch_bx);
holder.ch_bx.setTag(false);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
resultp = data.get(position);
if((Boolean)holder.ch_bx.getTag())
{
holder.ch_bx.setChecked(true);
}
else
{
holder.ch_bx.setChecked(false);
}
holder.ch_bx.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(holder.ch_bx.isChecked())
{
holder.ch_bx.setTag(true);
}
else
{
holder.ch_bx.setTag(false);
}
}
});
//Picasso.with(context).load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim()).into(flag);
Picasso.with(context)
.load("http://10.0.2.2:3009/buffet_items/".trim()+resultp.get("item_image").trim())
.resizeDimen(R.dimen.wanted_size,R.dimen.wanted_size).centerCrop().into(holder.flag);
return itemView;
}
class ViewHolder
{
ImageView flag;
CheckBox ch_bx;
}
来源:https://stackoverflow.com/questions/24015510/how-to-retain-the-checkbox-state-on-scroll-of-gridview-in-android