Recycler View Data未获得更新?

时间:2018-02-18 13:35:20

标签: android android-recyclerview adapter android-viewholder

我在片段中有一个回收站视图,它有按钮列表。当用户按下按钮时,它会打开另一个活动来编辑名称和其他数据。当它返回到片段时,它无法更新名称文本视图以及当我删除项目时,只要我在列表中保存新项目,它就会显示旧名称。请帮我解决。

public void updateScreen(String name, int pos) {
        modelList.add(new DataModel(name, R.drawable.custom_mode));
        mAdapter.notifyItemInserted(pos);
        mAdapter.notifyDataSetChanged();
    }

    public void deleteDataUi(int pos) {
        Log.e("DelteUIpos:", String.valueOf(pos));
        modelList.remove(pos);
        mrecycler.removeViewAt(pos);
        mAdapter.notifyItemRemoved(pos);
        mAdapter.notifyItemRangeChanged(pos, dataList.size());
        mAdapter.notifyDataSetChanged();

    }

    public void updateNameUI(int pos, String nm) {
        dataList.get(pos).setImageName(nm);
        mAdapter.notifyItemChanged(pos);
        mAdapter.notifyDataSetChanged();
    }

第二项活动

        case R.id.saveMode:
            MainFragm fragMain = new MainFragm ();
                    fragMain .updateScreen(edname, position);
                    NavUtils.navigateUpFromSameTask(secondActivity.this);
                    finish();
               break;
        case R.id.EditMode:
            MainFragm fragMain = new MainFragm ();
           fragMain.updateNameUI(position,edname.getText().toString());
                NavUtils.navigateUpFromSameTask(secondActivity.this);
                finish();
            break;
        case R.id.deleteMode:
            MainFragm fragMain = new MainFragm ();
            fragMain.deleteDataUi(Dataposition);
            NavUtils.navigateUpFromSameTask(secondActivity.this);
            finish();
            break;

适配器

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter .Holder> {
    private ArrayList<DataModel> dataModels = new ArrayList<>();
    private Context mContext;

    CustomAdapter (Context context, List<DataModel> dataModelsList) {
        mContext = context;
        dataModels = (ArrayList<DataModel>) dataModelsList;
    }

    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Inflate the layout, initialize the View Holder
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row, parent, false);
        return new CustomAdapter.ViewHolder(v);

    }

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

        //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
        holder.imageView.setImageResource(dataModels .get(position).getImg());
        holder.textView.setText(dataModels .get(position).getTextName());
    }

    @Override
    public int getItemCount() {
        //returns the number of elements the RecyclerView will display
        return dataModels.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        ImageButton imageView;
        TextView textView;

        ViewHolder (View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img_view);
            textView = itemView.findViewById(R.id.edit_tv);
            int width = 250;
            int height = 250;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
            imageView.setLayoutParams(params);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试在dataModelsList中为CustomAdapter创建一个返回参考数据集的公共getter。

然后使用modelList.remove(pos);

使用它:

mAdapter.createdGetterMethod().remove(pos)

改为使用 dataList.get(pos).setImageName(nm);使用此:

mAdapter.createdGetterMethod().get(pos).setImageName(nm);