从自定义适配器listview android更新图像

时间:2017-04-17 23:06:01

标签: android listview

我需要更新包含图像和按钮的listview,问题比我有索引,app总是更新listview的第二个索引,如果我有2行(imagen + button each),总是使用两个按钮更新第二个图像,2个按钮仅更新第二个图像而第一个不更新。

这是我的代码:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewclientes, null);
            holder = new CustomAdapterPublicidad.ViewHolder();

            holder.publicidadboton = (Button) convertView
                    .findViewById(R.id.publicidadboton);
            holder.publicidadimagen = (ImageView) convertView
                    .findViewById(R.id.publicidadimagen);


            final RowItemPublicidad row_pos = rowItems.get(position);

            holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());
            holder.publicidadboton.setText(row_pos.getBoton());

            convertView.setTag(holder);
        } else {
            holder = (CustomAdapterPublicidad.ViewHolder) convertView.getTag();
        }

        holder.publicidadboton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(position==0){
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda1").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();

                }else if(position==1) {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda2").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

}

2 个答案:

答案 0 :(得分:0)

您的问题是点击按钮后if position(int)onClick()运行,到那时位置始终为1.而是在点击按钮之前更改onClick ()内的内容。 (虽然这个位置是你想要获得的价值)

将您的代码更改为:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewclientes, null);
            holder = new CustomAdapterPublicidad.ViewHolder();    
            holder.publicidadboton = (Button) convertView
                    .findViewById(R.id.publicidadboton);             
            holder.publicidadimagen = (ImageView) convertView
                    .findViewById(R.id.publicidadimagen);



            final RowItemPublicidad row_pos = rowItems.get(position);

            holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());
            holder.publicidadboton.setText(row_pos.getBoton());

            convertView.setTag(holder);
        } else {
            holder = (CustomAdapterPublicidad.ViewHolder) convertView.getTag();
        }

        if (position==0) {
            holder.publicidadboton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda1").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();    
                }
            }
        }) else if (position==1) {
            holder.publicidadboton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda2").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();  
                }
            }
        };

        return convertView;
    }

}

答案 1 :(得分:0)

onBindViewHolder内或viewHolder类内写@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { holder.publicidadboton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(position==0){ FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); Bitmap myBitmap = QRCode.from(user.getUid() + "tienda1").withHint(EncodeHintType.MARGIN, 0).bitmap(); holder.publicidadimagen.setImageBitmap(myBitmap); notifyDataSetChanged(); }else if(position==1) { FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); Bitmap myBitmap = QRCode.from(user.getUid() + "tienda2").withHint(EncodeHintType.MARGIN, 0).bitmap(); holder.publicidadimagen.setImageBitmap(myBitmap); notifyDataSetChanged(); } } }); } 项。

bins