有按钮可以删除recyclerview中的所有内容,但是添加新的cardview时,相同的cardviews

时间:2018-10-03 03:13:57

标签: android android-recyclerview

注意:我以前在deleting all cardviews from recyclerview then adding new cardviews - adds same cardviews这里遇到过这个问题

当用户按下FAB时,卡片视图将添加到回收站视图。每个Cardview内部都有一个复选框。我要这样做,以便当用户在特定卡片视图中打勾时,该特定卡片视图被删除。

(仅供参考,每个Cardview都有一个微调框和一个复选框,以及一个textview和edittext)

我按了4次fab键,以将4张卡片视图添加到屏幕上。然后,我从微调器中挑选了不同的东西,看看发生了什么。

然后在我的溢出菜单中,单击显示删除列表的按钮。而且它会删除所有卡片视图,因此我的屏幕现在空白,这很好,

但是当我开始添加更多卡片视图时,我注意到它在微调器中的值与应该删除的值相同。

我在适配器中有这样的方法

public void reset() { 
   productList.clear(); 
   notifyDataSetChanged(); 
 }

就像我上面链接的问题答案一样,我尝试添加

holder.spinner.setSelection(0);

所以后来变成了

public void reset() {


        holder.spinner.setSelection(0);

        productList.clear();
        notifyDataSetChanged();

    }

但是现在它说cannot resolve symbol holder

我该怎么办?

这是我完整的 productadapter .java代码

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();

    private Map<String, String> numberItemValues = new HashMap<>();

    private List<Product> productList;

    private Activity create;


    //TODO CODE FOR CSV FILE


    InputStream inputStream = null;
    List<String>  mSpinnerItems = null;
    CSVFile csvFile = null;


    //TODO END OF CODE FOR CSV FILE

    public ProductAdapter(Activity activity) {
        create = activity;

    }



    public ProductAdapter(Activity activity, List<Product> productList, Map<String, String> numberList) {
        numberItemValues = numberList;
        create = activity;
        this.productList = productList;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(create);
        View view = inflater.inflate(R.layout.layout_products, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, final int position) {


        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(create, R.layout.item_spinner_layout,
                Product.getSpinnerItemsList());
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        holder.spinner.setAdapter(spinnerArrayAdapter);

        holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
                mSpinnerSelectedItem.put(position, mPosition);

                TextView mTextView = view.findViewById(R.id.mSpinnerText);

                String currentItem = holder.spinner.getItemAtPosition(mPosition).toString();

                Set<String> set = numberItemValues.keySet(); for(String key : set) {String value = numberItemValues.get(key); Log.e("DATA ", "key = " + key + " value = " + value); }

                String aisleNumber = numberItemValues.get(currentItem);
                holder.textView5.setText(aisleNumber);

                Log.e("SELECTION TEST", " Selected map item = " + aisleNumber );

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //binding the data with the viewholder views
        if (mSpinnerSelectedItem.containsKey(position)) {
            holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
        }


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


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);


                // set title
                alertDialogBuilder.setTitle("Delete Item");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Are you sure you want to delete this item?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, close
                                // current activity


                                holder.checkBox.setChecked(false);
                                holder.spinner.setSelection(0);

                                productList.remove(holder.getAdapterPosition());
                                notifyItemRemoved(holder.getAdapterPosition());

                                Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();


                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });

    }


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

    public void reset() {


        holder.checkBox.setChecked(false);
        holder.spinner.setSelection(0);

        productList.clear();
        notifyDataSetChanged();

    }

    class ProductViewHolder extends RecyclerView.ViewHolder {

        SearchableSpinner spinner;
        EditText editText;
        TextView textView5;
        CheckBox checkBox;
        LinearLayout linearLayout;
        View rootView;


        public ProductViewHolder(View itemView) {
            super(itemView);

            spinner = itemView.findViewById(R.id.spinner);
            editText = itemView.findViewById(R.id.editText);
            textView5 = itemView.findViewById(R.id.textView5);
            checkBox = itemView.findViewById(R.id.checkBox);
            rootView = itemView.findViewById(R.id.linearLayout);


            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // makes the set disappear when checkbox is ticked.
                    if(isChecked){

                        checkBox.setChecked(false);
                        spinner.setSelection(0);

                        productList.remove(getAdapterPosition());
                        notifyItemRemoved(getAdapterPosition());



                        Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
                    }

                }
            });



        }

        public View getView() {
            return rootView;
        }

    }


    //TODO CODE FOR CSV FILE
    private class CSVFile {
        InputStream inputStream;

        public CSVFile(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public List<String> read() {
            List<String> resultList = new ArrayList<String>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] row = line.split(",");

                    numberItemValues.put(row[1], row[0]);

                    resultList.add(row[1]);
                }
            } catch (IOException e) {
                Log.e("Main", e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e("Main", e.getMessage());
                }
            }
            return resultList;
        }
    }


}

1 个答案:

答案 0 :(得分:0)

我认为这里的问题是recyclerview正在重用已经创建的视图。

在ViewHolder中创建一个重置方法

public void reset() {
   textview5.setText("");
   edittext.setText("");
   // Add other variable too if needed
}

然后在设置数据之前在onBindView()中调用此方法。

@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {

    // reseting view
    holder.reset();

   // rest of your code...
}