GridView适配器无法正常工作

时间:2015-12-02 12:26:49

标签: android gridview adapter

我在android中学习使用gridview而且我不知道如何解决一个问题...... 我尝试使用gridview创建管理器应用,但我不知道代码中的问题在哪里。

StickerGridViewAdapter:

public class StickerGridViewAdapter extends ArrayAdapter<Sticker> {

Context context;
int layoutResourceId;
List<Sticker> data = new ArrayList<Sticker>();
RecordHolder viewHolder = new RecordHolder();

public StickerGridViewAdapter(Context context, int layoutResourceId, List<Sticker> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Sticker sticker = data.get(position);
    if (convertView == null) {
        viewHolder = new RecordHolder();
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);

        viewHolder.count = sticker.getAmount();
        viewHolder.textViewNumber = (TextView) convertView.findViewById(R.id.textViewNumber);
        viewHolder.textViewQuantity = (TextView) convertView.findViewById(R.id.textViewQuantity);
        viewHolder.iv_logo_card = (ImageView) convertView.findViewById(R.id.iv_logo_card);
        viewHolder.buttonAdd = (ImageButton) convertView.findViewById(R.id.buttonAdd);
        viewHolder.buttonRemove = (ImageButton) convertView.findViewById(R.id.buttonRemove);
        viewHolder.textViewQuantity.setText(sticker.getAmount().toString());
        paintQuantity(viewHolder.count, viewHolder, context);
        convertView.setTag(viewHolder);

    } else {
        viewHolder = (RecordHolder) convertView.getTag();
    }

    viewHolder.buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (viewHolder.count != 99) {
                viewHolder.count++;
            }
            sticker.setAmount(viewHolder.count);
            StickerBusinessService.saveSticker(sticker);
            viewHolder.textViewQuantity.setText(sticker.getAmount().toString());
            paintQuantity(viewHolder.count, viewHolder, context);
        }
    });

    viewHolder.buttonRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (viewHolder.count != 0) {
                viewHolder.count--;
            }
            sticker.setAmount(viewHolder.count);
            StickerBusinessService.saveSticker(sticker);
            viewHolder.textViewQuantity.setText(sticker.getAmount().toString());
            paintQuantity(viewHolder.count, viewHolder, context);
        }
    });


    viewHolder.textViewNumber.setText(sticker.getNumber().toString());
    return convertView;

}

private static class RecordHolder {
    TextView textViewNumber;
    TextView textViewQuantity;
    ImageView iv_logo_card;
    ImageButton buttonAdd;
    ImageButton buttonRemove;
    int count;
}

public static void paintQuantity(int count, RecordHolder holder, Context context){
    if(count == 0){
        holder.textViewQuantity.setVisibility(View.INVISIBLE);
    }else if(count == 1){
        holder.textViewQuantity.setVisibility(View.VISIBLE);
        holder.textViewQuantity.setTextColor(ContextCompat.getColor(context, R.color.material_green_500));
    }else if(count > 1){
        holder.textViewQuantity.setTextColor(ContextCompat.getColor(context, R.color.material_amber_600));
    }
}

}

itensgridview)有两个按钮(ADD&#34; +&#34;以及SUBTRACT&#34; -&#34;),但是当我点击按钮时,该项(id=1)会收到count = 1,但如果我向下滚动其他itens则会收到相同的计数......我做错了什么?

2 个答案:

答案 0 :(得分:0)

您不应在getView之外创建RecordHolder的实例。你已经在getView()中创建了。

在getView()中创建RecordHolder的实例,如。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   final Sticker sticker = data.get(position);
   RecordHolder viewHolder;
   if (convertView == null) {
      viewHolder = new RecordHolder();
      //remaining code is here
   }else {
     //else part
   }
  return converview;
}

答案 1 :(得分:0)

我做了Saritha G所说的及其作品! 现在我的代码是这样的:

public class StickerGridViewAdapter extends ArrayAdapter<Sticker> {

Context context;
int layoutResourceId;
List<Sticker> data = new ArrayList<Sticker>();

public StickerGridViewAdapter(Context context, int layoutResourceId, List<Sticker> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final RecordHolder holder;
    final Sticker sticker = data.get(position);
    if (row == null) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder();
        holder.count = sticker.getAmount();
        holder.textViewNumber = (TextView) row.findViewById(R.id.textViewNumber);
        holder.textViewQuantity = (TextView) row.findViewById(R.id.textViewQuantity);
        holder.iv_logo_card = (ImageView) row.findViewById(R.id.iv_logo_card);
        holder.buttonAdd = (ImageButton) row.findViewById(R.id.buttonAdd);
        holder.buttonRemove = (ImageButton) row.findViewById(R.id.buttonRemove);
        holder.textViewQuantity.setText(sticker.getAmount().toString());
        paintQuantity(holder.count, holder, context);
        row.setTag(holder);

    } else {
        holder = (RecordHolder) row.getTag();
    }

    holder.buttonAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.count != 99) {
                holder.count++;
            }
            //holder.textViewQuantity.setText("" + holder.count);
            sticker.setAmount(holder.count);
            StickerBusinessService.saveSticker(sticker);
            holder.textViewQuantity.setText(sticker.getAmount().toString());
            paintQuantity(holder.count, holder, context);
        }
    });

    holder.buttonRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.count != 0) {
                holder.count--;
            }
            //holder.textViewQuantity.setText("" + holder.count);
            sticker.setAmount(holder.count);
            StickerBusinessService.saveSticker(sticker);
            holder.textViewQuantity.setText(sticker.getAmount().toString());
            paintQuantity(holder.count, holder, context);
        }
    });


    holder.textViewNumber.setText(sticker.getNumber().toString());
    //
    return row;

}

private static class RecordHolder {
    TextView textViewNumber;
    TextView textViewQuantity;
    ImageView iv_logo_card;
    ImageButton buttonAdd;
    ImageButton buttonRemove;
    int count;
}

public static void paintQuantity(int contador, RecordHolder holder, Context context){
    if(contador == 0){
        holder.textViewQuantity.setVisibility(View.INVISIBLE);
    }else if(contador == 1){
        holder.textViewQuantity.setVisibility(View.VISIBLE);
        holder.textViewQuantity.setTextColor(ContextCompat.getColor(context, R.color.material_green_500));
    }else if(contador > 1){
        holder.textViewQuantity.setTextColor(ContextCompat.getColor(context, R.color.material_amber_600));
    }
}
}

但是当我向下滚动网格时,每个项目的价值都在重复......我该怎么办才能修复它?