簡體   English   中英

將RecyclerView項目添加到recyclerView時,如何重置數據或設置其默認數據?

[英]How to reset data or set default data on a RecyclerView item when it is added to the recyclerView?

我有一個textView擁有一個數值,當單擊添加按鈕時,它會向其中添加一個,而當單擊減法按鈕時,它會從中減去1。 但是,當我向recyclerView添加新項目時,數字值將自動設置為最后一個項目值。 因此,如果第一個項目numberValue為3,則第二個項目編號值在添加時將為3。 當新項目添加到recyclerView時,我希望textView自動為0

public class PlatesAdapter extends
    RecyclerView.Adapter<PlatesAdapter.ViewHolder> {

//Declaring a List<> of Plates
private List<Plates> mPlatesList;

//Variable to hold total numberOfPlates being used
int amountOfPlates = 0;
String amountOfPlatesString;

//OnBindViewHolder
@Override
public void onBindViewHolder(PlatesAdapter.ViewHolder holder, int position) {

    final TextView amountOfPlatesTextView = holder.amountOfPlatesTextView;

    //BUTTONS add 1 or subtract 1 from amountOfPlates;
    Button addAmountOfPlatesButton = holder.addButton;
    Button subtractAmountOfPlatesButton = holder.subButton;

    addAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addPlates();
            amountOfPlatesString = Integer.toString(amountOfPlates);
            amountOfPlatesTextView.setText(amountOfPlatesString);
        }
    });

    subtractAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            subtractPlates();
            amountOfPlatesString = Integer.toString(amountOfPlates);
            amountOfPlatesTextView.setText(amountOfPlatesString);
        }
    });
}

public void addPlatesLayout(Plates plate) {
    if (mPlatesList == null) mPlatesList = new ArrayList();
    //plate.setPlateWeight(a);
    mPlatesList.add(plate);
    //notifyDataSetChanged();
    notifyItemInserted(mPlatesList.size() - 1);
}

public int addPlates() {
    amountOfPlates++;
    return amountOfPlates;
}

public int subtractPlates() {
    amountOfPlates--;
    return amountOfPlates;
}

只需在OnBindViewHolder中設置setText。

@Override
public void onBindViewHolder(PlatesAdapter.ViewHolder holder, int position) {

    final TextView amountOfPlatesTextView = holder.amountOfPlatesTextView;
    amountOfPlatesTextView.setText("0");
    //BUTTONS add 1 or subtract 1 from amountOfPlates;
    Button addAmountOfPlatesButton = holder.addButton;
    Button subtractAmountOfPlatesButton = holder.subButton;

    addAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addPlates();
            amountOfPlatesString = Integer.toString(amountOfPlates);
            amountOfPlatesTextView.setText(amountOfPlatesString);
        }
    });

    subtractAmountOfPlatesButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            subtractPlates();
            amountOfPlatesString = Integer.toString(amountOfPlates);
            amountOfPlatesTextView.setText(amountOfPlatesString);
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM