简体   繁体   中英

Add item to RecyclerView when a Button is clicked

I want to add items to RecyclerView when I click the "Add" button

This currently works but only once , meaning if I click the Add button the first time, the item is added and visible, but after that, nothing is added.

Here is my code for RecyclerView Adapter

public class Adapter extends RecyclerView.Adapter<Adapter.myViewHolder> {

    List<Integer> listItem;

    public Adapter(List<Integer> passedListItem){
        this.listItem = passedListItem;
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_layout, parent, false);

        myViewHolder holder = new myViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(myViewHolder holder, int position) {
        int itemNumber = position+1;
        holder.itemTextView.setText("Item Number " + itemNumber + ": " + listItem.get(position));
    }

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

    public class myViewHolder extends RecyclerView.ViewHolder {
        TextView itemTextView;

        public myViewHolder(View view){
            super(view);
            itemTextView = view.findViewById(R.id.tv_itemTextView);
        }
    }
}

Here's my MainActivity

public class MainActivity extends AppCompatActivity {

    List<Integer> itemList = new ArrayList<>();
    EditText itemEditText;

    RecyclerView recyclerView;
    Adapter rvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView)findViewById(R.id.rv_itemsRecyclerView);
        itemEditText = (EditText)findViewById(R.id.et_editText);

        //Setting the layout and Adapter for RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rvAdapter = new Adapter(itemList);
        recyclerView.setAdapter(rvAdapter);
    }

    //Click listener for "Add" Button
    public void onAddButtonClicked(View view) {

        try {
            int IntegerFormat = Integer.valueOf(itemEditText.getText().toString());
            itemList.add(IntegerFormat);
            rvAdapter.notifyItemInserted(itemList.size() - 1);
            itemEditText.setText("");
        } catch(NumberFormatException e) {
            Toast.makeText(getApplicationContext(), "The field is empty",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

When i click the Add button, the first item is added and is visible, but when I click the Add button second time, nothing happens.


Solved

EDIT: Apparently, my Recycler view layout had its width and height set to match_parent instead of wrap_content, so the second item was getting added after clicking the button but it was added way way below. And I was stupid enough to not even try to scroll down. Everything was just working fine but I was ignorent.

I just checked your code and it is working fine. The mistake is in the recyclerview_layout.xml file which you havent posted. What you have is this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_itemTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TextView" />
</LinearLayout>

Please change this to:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_itemTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

Your first list item is filling the entire recycler view because your linear layout has android:layout_height="match_parent" instead of android:layout_height="wrap_content" so the first item is hiding the other items but they are there in the adapter. You can confirm this by logging in logcat.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM