在列表中添加新项目时如何更新RecyclerView适配器数据

时间:2018-09-12 11:48:37

标签: android android-layout listview android-fragments android-recyclerview

我正在研究将FooterView添加到RecyclerView的功能。我通过在FooterView中单击“ +”图标在列表中添加新项目。从片段成功添加到ArrayList中的项目,数据已添加到本地数据库,但是新列表项尚未添加到列表中,因为适配器无法同时更新。当我转到另一个屏幕时,请返回此屏幕,然后可以看到该项目。我曾尝试这样做,但未能做到。

适配器的代码:`公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {

private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;


private Activity activity;

ArrayList<ModelUnrecognisedCode> modelUnrecognisedCodesArrayList;
FinalizePreOrde finalizePreOrderFragment;
ModelUnrecognisedCode modelUnrecognisedCode;

public RecyclerViewAdapter(Activity activity, ArrayList<ModelUnrecognisedCode> modelUnrecognisedCodesArrayList, FinalizePreOrde finalizePreOrderFragment) {
    this.activity = activity;
    this.modelUnrecognisedCodesArrayList = modelUnrecognisedCodesArrayList;
    this.finalizePreOrderFragment = finalizePreOrderFragment;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_unrecognised_item, parent, false);
        return new ItemViewHolder(itemView);
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer_view, parent, false);
        return new FooterViewHolder(itemView);
    } else return null;
}

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


    if (holder instanceof FooterViewHolder) {
        final FooterViewHolder footerHolder = (FooterViewHolder) holder;

        footerHolder.imgAddMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finalizePreOrderFragment.addView(footerHolder.etCode.getText().toString(), position);

            }
        });


    } else if (holder instanceof ItemViewHolder) {
        final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        modelUnrecognisedCode = modelUnrecognisedCodesArrayList.get(position);
        itemViewHolder.etCode.setText(modelUnrecognisedCode.getUnrecognised_code());


    }
}

@Override
public int getItemViewType(int position) {
    if (position == modelUnrecognisedCodesArrayList.size()) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

@Override
public int getItemCount() {
    return modelUnrecognisedCodesArrayList.size() + 1;
}

`

在页脚部分的BindView Holder中,我添加了代码以单击“ +”图标并在列表中添加新项

footerHolder.imgAddMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finalizePreOrderFragment.addView(footerHolder.etCode.getText().toString(), position);

            }
        });

现在是我的Fragment代码以添加新的

 public void addView(String code, int position) {
    ModelUnrecognisedCode modelUnrecognisedCode = new ModelUnrecognisedCode();
    modelUnrecognisedCode.setUnrecognised_code(code);
    modelUnrecognisedCode.setUnrecognised_comment("comment");
    modelUnrecognisedCode.setShop_id("1");
    modelUnrecognisedCode.setShop_name_fr("fr");
    modelUnrecognisedCode.setShop_name_nl("nl");
    databaseHelper.addUnrecognisedCode(modelUnrecognisedCode);
    modelUnrecognisedCodesArrayList = databaseHelper.getUnrecognisedCode();

    adapterUnrecognisedCode.notifyItemInserted(position);
    adapterUnrecognisedCode.notifyItemRangeChanged(position, modelUnrecognisedCodesArrayList.size());
    adapterUnrecognisedCode.notifyDataSetChanged();

    Log.e("TAG", "unrecognised code: " + modelUnrecognisedCodesArrayList.size());
}

我能够在ArrayList中添加该项目,但是当我按下然后再次返回然后我能够看到该项目时,适配器未更新。请帮助

3 个答案:

答案 0 :(得分:1)

尝试清除您的列表,再次添加所有项目,然后通知更改。像这样:

list.clear()
list.addAll(yourUpdatedList)
adapter.notifyDataSetChanged()

答案 1 :(得分:0)

您尝试过:RecyclerView.adapter.notifyDataSetChanged() 它对我有用,可以直接更新您的RecyclerView

答案 2 :(得分:0)

如果我收到您的问题,您就不会在列表中添加新项目   将新添加的项目添加到列表中,然后notifychanged

  

modelUnrecognisedCodesArrayList .add(modelUnrecognisedCode);

 public void addView(String code, int position) {
        ModelUnrecognisedCode modelUnrecognisedCode = new ModelUnrecognisedCode();
        modelUnrecognisedCode.setUnrecognised_code(code);
        modelUnrecognisedCode.setUnrecognised_comment("comment");
        modelUnrecognisedCode.setShop_id("1");
        modelUnrecognisedCode.setShop_name_fr("fr");
        modelUnrecognisedCode.setShop_name_nl("nl");
        databaseHelper.addUnrecognisedCode(modelUnrecognisedCode);
        modelUnrecognisedCodesArrayList = databaseHelper.getUnrecognisedCode();

      modelUnrecognisedCodesArrayList .add(modelUnrecognisedCode);

      adapterUnrecognisedCode.notifyItemInserted(position);
        adapterUnrecognisedCode.notifyItemRangeChanged(position, 
     modelUnrecognisedCodesArrayList.size());
        adapterUnrecognisedCode.notifyDataSetChanged();

        Log.e("TAG", "unrecognised code: " + modelUnrecognisedCodesArrayList.size());
    }