何根据适配器回调列表器更新适配器中的视图持有者文本视图值?

时间:2018-01-03 04:09:59

标签: java android adapter

我有一个循环视图适配器,它包含一个文本视图,适配器从一个对话框片段获取回调值。我想在获取回调值时将setText设置为持有者的textview。在returnData方法中我得到回调value,现在我想在我的持有人文本视图中设置名为text value的值。

 public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener,MyDialogFragment.OnSetChangeListenerDialog{
     @Override
      public void returnData(String result) {
    setValue = result;
      }

       }

public MyAdapter(Activity activity, List<Detail> list, FragmentManager fragmentManager) {

    this.activity = activity;
    this.list = list;
    this.fragmentManager = fragmentManager;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.item, parent, false);

    final ListItemViewHolder viewHolder = new ListItemViewHolder(view);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            position = viewHolder.getAdapterPosition();
        }
    });

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

    try {

        final ListItemViewHolder viewHolder = (ListItemViewHolder) holder;

        fina Detail item = list.get(position);
           viewHolder.textvalueTwo .setText(item.DESC());
        }


@Override
public void returnData(String result) {
    setValue = result;
}


   public class ListItemViewHolder extends RecyclerView.ViewHolder {
     private TextView textvalue;
        public ListItemViewHolder(final View parent) {
        super(parent);
          textvalue = (EditText) parent.findViewById(R.id.textvalue);
            textvalueTwo = (TextView) parent.findViewById(R.id.textvalueTwo);

            }
}}

3 个答案:

答案 0 :(得分:0)

您是否尝试使用list.set (index,model)

 //Updating a particular item
public void updateItem(int index, YourModelClass item) {
    mList.set(index, item);
    notifyItemChanged(index);
}

这将仅更新RecyclerView的特定数据 试一试让我知道

更新

returnData()添加以下代码

Detail item = list.get(getAdapterPosition());
item .setDescription(result);// updating the description value

updateItem(getAdapterPosition(), item); // calling the method to notify the change

答案 1 :(得分:0)

你走了。当您通知数据集已更改时,将调用onBindViewHolder()方法。

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // update your callback value here
    if(!TextUtils.isEmpty(setValue)) {
        viewHolder.textvalue.setText(setValue);
    }
}

并在returnData()中:

@Override
public void returnData(String result) {
    setValue = result;
    notifyDataSetChanged();
    // If you know exactly position of item changed
    // use  notifyItemChanged(position of item changed);
}

答案 2 :(得分:0)

这应该有效

@Override
public void returnData(String result) {
    setValue = result;
    Detail item = list.get(position);
    item.DESC = setValue;
    mList.set(position, item);
    notifyItemChanged(position);
}