Jairo Jimenez
Jairo Jimenez

Reputation: 11

Adding a new item to a RecyclerView when button is clicked?

I just started using RecyclerViews but i cant completely understand how to add or remove items from it. Below i will attach my adapter code it is a test code and everything in the layout works fine. I feel like im also writing too much unnecessary code so any tips or criticism is appreciated.

public class PlatesAdapter extends
        RecyclerView.Adapter<PlatesAdapter.ViewHolder> {
    //Declaring a List<> of Plates
    private List<Plates> mPlates;
    int amountOfPlates;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        //Declaring Buttons and textViews
        public TextView plateWeightTextView, amountOfPlatesTextView;
        public Button addButton, subButton, addLayoutButton;


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

            //initializing Buttons and TextViews
            plateWeightTextView = (TextView) itemView.findViewById(R.id.plate_weight_value_textView);
            amountOfPlatesTextView = (TextView) itemView.findViewById(R.id.amount_of_plates_textView);
            addButton = (Button) itemView.findViewById(R.id.add_button);
            subButton = (Button) itemView.findViewById(R.id.subtract_button);
            addLayoutButton = (Button) itemView.findViewById(R.id.button);

        }
    }

    //Constructor
    public PlatesAdapter(List<Plates> plates) {
        mPlates = plates;
    }

    @Override
    public PlatesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View PlatesView = inflater.inflate(R.layout.plate_item_layout, parent, false);

        ViewHolder viewHolder = new ViewHolder(PlatesView);
        return viewHolder;
    }

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

        final TextView textView2 = holder.amountOfPlatesTextView;

        //BUTTONS add 1 or subtract 1 from amountOfPlates;
        Button button = holder.addButton;
        Button button2 = holder.subButton;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                amountOfPlates++;
                textView2.setText(Integer.toString(amountOfPlates));
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                amountOfPlates--;
                textView2.setText(Integer.toString(amountOfPlates));
            }
        });
    }

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

Here is my Model Layer which i feel is completely wrong but im not 100% sure if it is.

  public class Plates {
    private int mPlateWeight;
    private int mAmountOfPlates;

    public Plates() {
        //mPlateWeight = plateWeight;
        //mAmountOfPlates = amountOfPlates;
    }

    public int getmPlateWeight() {
        return mPlateWeight;
    }

    public int getmAmountOfPlates() {
        return mAmountOfPlates;
    }

    public static List<Plates> createPlateList() {
        List<Plates> plates = new ArrayList<>();

        plates.add(new Plates());

        return plates;
    }
}

This is where im comfused. Its were do i call the addPlates or addItem method and what do i pass to it? Below is my main activity. I Just dont know where to add these addItems or addPlates methods is it to the Model Layer or the Adapter?

public class MainActivity extends AppCompatActivity {

private RecyclerView.LayoutManager mLayoutManager;

Button mButton;

private List<Plates> mData = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Button to add layout to recyclerView
    mButton = (Button) findViewById(R.id.button);
    //Adapter LayoutManager
    mLayoutManager = new LinearLayoutManager(this);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.weights_recycler_view);
            PlatesAdapter adapter = new PlatesAdapter(mData);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(mLayoutManager);
        }
    });
}

}

Upvotes: 1

Views: 9070

Answers (3)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364858

You can add a method in your adapter to add a Plates in the arrayList and to notify the change. Something like:

public void addPlates(Plates plate) {
  if (mPlates == null) mPlates = new ArrayList();
  mPlates.add(plate);
  //notifyDataSetChanged();
  notifyItemInserted(mPlates.size()-1)
}`

Upvotes: 1

JeanCarrot
JeanCarrot

Reputation: 41

What I usually do with RecyclerViews is to add a method to set the data. In your example :

public void setPlates(List<Plates> plates) {
  mPlates = plates;
  notifyDataSetChanged();
}`

You can also add a getter if you want to verify if the data have changed or not.

Upvotes: 1

Drilon Blakqori
Drilon Blakqori

Reputation: 2826

First of all the createPlateList method is not needed. You should add a method in your adapter that looks like this:

public void addItem(Plates plate)
{
    mPlates.add(plate);
}

Since your adapter works with this list, all you need to do to add or remove items, is to add/remove the items from your list. After all you need to call notifyDataSetChanged() in your adapter so it knows data was changed in your list.

Upvotes: 0

Related Questions