我通过捆绑使用数据传输,但由于某种原因,数据只传输了 1 次

I use data transfer via bundle, but for some reason the data is transmitted only 1 time

我用这段代码发送数据

 @Override
public void onClick(View v) {
    DialogFragment dlg;
    dlg = new barcodeView();
    Coupons coupons = coupons2.get(position);
    String i = coupons.getTAG();

    Log.v("dwdwdw",""+i);
    Bundle bundle = new Bundle();
    if (i.equals("lentaAmount500")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "5000112592702");

        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("lentaAmount300")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054490086");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("lentaAmount100")){
        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054491472");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount500")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054492387");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount300")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0049000065206");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount100")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "050051000481");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }

等获得barcode_data = bundle.getString("someValue");

但是为什么第一次一切正常,数据发送了,但是下一次点击显示的是旧数据,即发送的数据没有变化。如何清洁它们? 我检查了调试模式,发现 Coupons coupons = coupons 2.get(position); 没有改变。为什么会发生这种情况以及如何解决?

我发送数据的所有 Recycler 适配器:

class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> implements View.OnClickListener {

private Context context;
private LayoutInflater inflater;
private List<Coupons> coupons2;
 RecyclerAdapter(Context context, List<Coupons> coupons2){

    this.coupons2 = coupons2;
    this.inflater = LayoutInflater.from(context);
    this.context = context;










}


@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = inflater.inflate(R.layout.coupon,parent,false);

    return new ViewHolder(view);
}
 private int position;
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
    Coupons coupons = coupons2.get(position);
    this.position = position;

    viewHolder.imageView.setImageResource(coupons.getLogo());
    viewHolder.imageView.setOnClickListener(this);





}
Coupons coupons;

@Override
public int getItemCount(){
    return coupons2.size();
}

@Override
public void onClick(final View v) {
    DialogFragment dlg;
    dlg = new barcodeView();
    if (coupons!=null){
        coupons = null;
    }

    Coupons coupons = coupons2.get(position);


    String i = coupons.getTAG();

    Log.v("dwdwdw",""+i);
    Bundle bundle = new Bundle();
    if (i.equals("lentaAmount500")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "5000112592702");

        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("lentaAmount300")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054490086");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("lentaAmount100")){
        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054491472");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount500")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0000054492387");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount300")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "0049000065206");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");
    }
    if (i.equals("petiarochkaAmount100")){

        FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
        bundle.clear();
        bundle.putString("someValue", "050051000481");
        dlg.setArguments(bundle);
        dlg.show(fragmentManager,"barcodeview");

    }


}

static class ViewHolder extends RecyclerView.ViewHolder{
    final ImageView imageView;

    ViewHolder(View v){
        super(v);
        imageView = v.findViewById(R.id.logo);
    }
}

我获取数据的所有 DialogFragment:

 public class barcodeView extends DialogFragment implements 
View.OnClickListener {

ImageView barcodeview;
TextView barcodetextView;
String barcode_data;
// barcode image
Bitmap bitmap = null;


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getDialog().setTitle("Ваш штрихкод");
    View v = inflater.inflate(R.layout.barcode_view, null);
    Bundle bundle = this.getArguments();
    barcode_data = bundle.getString("someValue");
    barcodetextView = v.findViewById(R.id.textViewbarcode);
    barcodeview =  v.findViewById(R.id.barcodeView);

    try {
        bitmap = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 600, 300);
        barcodeview.setImageBitmap(bitmap);
        barcodetextView.setText(barcode_data);
    } catch (WriterException e) {
        e.printStackTrace();
    }


    return v;

}
Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException {
    String contentsToEncode = contents;
    if (contentsToEncode == null) {
        return null;
    }
    Map<EncodeHintType, Object> hints = null;
    String encoding = guessAppropriateEncoding(contentsToEncode);
    if (encoding != null) {
        hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
        hints.put(EncodeHintType.CHARACTER_SET, encoding);
    }
    MultiFormatWriter writer = new MultiFormatWriter();
    BitMatrix result;
    try {
        result = writer.encode(contentsToEncode, format, img_width, img_height, hints);
    } catch (IllegalArgumentException iae) {
        // Unsupported format
        return null;
    }
    int width = result.getWidth();
    int height = result.getHeight();
    int[] pixels = new int[width * height];
    for (int y = 0; y < height; y++) {
        int offset = y * width;
        for (int x = 0; x < width; x++) {
            pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
        }
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
}

private static String guessAppropriateEncoding(CharSequence contents) {
    // Very crude at the moment
    for (int i = 0; i < contents.length(); i++) {
        if (contents.charAt(i) > 0xFF) {
            return "UTF-8";
        }
    }
    return null;
}


public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
   barcodeview = null;
   barcodetextView = null;
   barcode_data = null;
   Log.v("dwdw","wdwdwd");


}



@Override
public void onClick(View v) {
    dismiss();

}

onClick

中用private int position;得到position不好

让我们尝试实现 OnClickListener 下面的代码:

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
    final Coupons coupons = coupons2.get(position);

    viewHolder.imageView.setImageResource(coupons.getLogo());
    viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO: you can use 'coupons' and 'position' declared as final here
            String i = coupons.getTAG();
        }
    });
}