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.
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. (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.
@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();
}
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.