简体   繁体   English

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

[英]update image from a custom adapter listview android

I need to update my listview that contain a image and a button, the problem than i have is with the index, the app always update the second index of the listview, if i have 2 rows (imagen+ button each),always the second imagen is updated using both buttons, the 2 buttons update only the second image and the first not. 我需要更新包含图像和按钮的列表视图,索引比我有的问题大,如果我有2行(每个imagen +按钮),应用程序总是更新列表视图的第二个索引,总是第二个imagen使用两个按钮更新,两个按钮仅更新第二个图像,第一个不更新。

this is my code: 这是我的代码:

@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;
    }

}

Your issue is the if position(int) inside onClick() is run once the button is clicked, and by that time position is always 1. Instead, change what onClick () does before the button is clicked. 您的问题是,一旦单击按钮,便会运行onClick()if position(int) ,并且此时位置始终为1。相反,请更改onClick ()在单击按钮之前的操作。 (while the position is the value you are trying get) (而排名是您尝试获取的值)

change your code to this: 将您的代码更改为此:

@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;
    }

}

Write the position check inside onBindViewHolder or inside the viewHolder class. onBindViewHolderviewHolder类内部编写position检查。

@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();
                }
            }
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM