有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java RecyclerView我最多只能添加一行

在我的RecyclerView中添加多行(项目)时遇到问题,当我点击按钮时,该项目会覆盖上一行(项目),但如果我创建了一个列表<&燃气轮机;对于onCreate上硬编码的数据,它可以在RecyclerView中添加多行数据。请遵循以下代码:

ListChecklistAdapter

public class ListChecklistAdapter extends RecyclerView.Adapter<ListChecklistAdapter.ListChecklistViewHolder> {
    private List<Checklist> mChecklist;
    private Context mCtx;

    public ListChecklistAdapter(Context ctx, List<Checklist> checklists) {
        mCtx = ctx;
        mChecklist = checklists;
    }

    @Override
    public ListChecklistViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.item_checklist, parent, false);
        return new ListChecklistViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ListChecklistViewHolder holder, int position) {
        holder.cbItem.setText(mChecklist.get(position).getDescricao());
        holder.cbItem.setChecked(mChecklist.get(position).isCheckado());
    }

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

    public class ListChecklistViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_checkbox)
        CheckBox cbItem;

        public ListChecklistViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void refreshData(List<Checklist> item) {
        this.mChecklist.clear();
        this.mChecklist.addAll(item);
        notifyDataSetChanged();
    }
}

item_checklist

<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content">

    <CheckBox
        安卓:id="@+id/item_checkbox"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="5dp"
        安卓:paddingLeft="6dp"
        安卓:text=" "
        tools:text="Exemplo de item de lista" />

</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.rcv_lista)
    RecyclerView rvLista;
    int valor;
    private List<Checklist> lista;
    private ListChecklistAdapter listChecklistAdapter;
    @BindView(R.id.btn_add_item)
    Button btnAdd;

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

    }

    private int cont() {
        return valor++;
    }

    public void novoItem(View view) {
        lista = new ArrayList<>();
        lista.add(new Checklist("Item " + (cont() + 1), true));
        loadRecycler(lista);
    }

    private void loadRecycler(List<Checklist> lista) {
        if (listChecklistAdapter == null) {
            Log.i("LOG", "IF");

            listChecklistAdapter = new ListChecklistAdapter(this, lista);

            rvLista.setAdapter(listChecklistAdapter);
            rvLista.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            rvLista.addItemDecoration(new DividerItemDecoration(this, 1));
            return;
        } else {
            Log.i("LOG", "ELSE");
            listChecklistAdapter.refreshData(lista);
            listChecklistAdapter.onAttachedToRecyclerView(rvLista);
        }
    }
}

activity_main

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical"
    安卓:padding="16dp"
    tools:context="teste.com.br.recyclercomcheckbox.MainActivity">

    <安卓.support.v7.widget.RecyclerView
        安卓:id="@+id/rcv_lista"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <Button
        安卓:id="@+id/btn_add_item"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="right"
        安卓:layout_marginTop="5dp"
        安卓:background="@安卓:color/transparent"
        安卓:onClick="novoItem"
        安卓:paddingLeft="20dp"
        安卓:paddingRight="20dp"
        安卓:text="+ novo item"
        安卓:textColor="#009688" />

</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    @Nilesh Rathod解决了这个问题!我刚刚从ListCheckAdapter.java > refreshData()中删除了this.mChecklist.clear(); 谢谢大家的帮助

  2. # 2 楼答案

    只需从refreshData()方法中删除这一行this.mChecklist.clear();

    public void refreshData(List<Checklist> item) {
        this.mChecklist.addAll(item);
        notifyDataSetChanged();
    }