单击一个按钮时,将项目添加到RecyclerView [英] Add item to RecyclerView when a Button is clicked

查看:114
本文介绍了单击一个按钮时,将项目添加到RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击添加"时,我想向RecyclerView添加项目.按钮

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.

这是我的RecyclerView Adapter代码

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);
        }
    }
}

这是我的MainActivity

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.

显然,我的Recycler视图布局的宽度和高度设置为match_parent而不是wrap_content,因此在单击按钮后添加了第二项,但它是通过以下方式添加的.而且我很愚蠢,甚至没有尝试向下滚动.一切都很好,但我却无视.

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.

推荐答案

我刚刚检查了您的代码,它工作正常.错误在于您尚未发布的 recyclerview_layout.xml 文件中.你有什么?

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>

请将其更改为:

<?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>

您的第一个列表项正在填充整个回收者视图,因为您的线性布局具有 android:layout_height ="match_parent" 而不是 android:layout_height ="wrap_content" 第一项隐藏其他项,但是它们在适配器中.您可以通过登录logcat来确认.

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.

这篇关于单击一个按钮时,将项目添加到RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆