如何在 android recyclerview 适配器中获取所有更改的项目

How to get all changed items in android recyclerview adapter

我在 RecyclerView 中使用名称和描述。

但是名称和描述有编辑文本,用户可以在其中更改名称、描述并提交整个字段。

现在的问题是如何获取 recyclerview 中所有项目的整个字段 android?

public static List<CityEvent> getData() {
        List<CityEvent> list = new ArrayList<>();
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Some event", "Some event in London", CityEvent.EVENT_TYPE));
        list.add(new CityEvent("Droidcon", "Droidcon in Berlin", CityEvent.EVENT_TYPE));
        return list;
    }



public class CityEvent {

    public static final int EVENT_TYPE = 1;

    private String mName;

    private String mDescription;

    private int mType;

    public CityEvent(String name, String description, int type) {
        this.mName = name;
        this.mDescription = description;
        this.mType = type;
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        this.mName = name;
    }

    public String getDescription() {
        return mDescription;
    }

    public void setDescription(String description) {
        this.mDescription = description;
    }

    public int getType() {
        return mType;
    }

    public void setType(int type) {
        this.mType = type;
    }
}

所以我把它放在编辑文本字段中。

public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CityEvent> mList;

    public DifferentRowAdapter(List<CityEvent> list) {
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        switch (viewType) {
            case CITY_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_city, parent, false);
                return new CityViewHolder(view);
            case EVENT_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_event, parent, false);
                return new EventViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CityEvent object = mList.get(position);
        if (object != null) {
            switch (object.getType()) {
                case EVENT_TYPE:
                    ((EventViewHolder) holder).mTitle.setText(object.getName()); //in edittext i am using name
                    ((EventViewHolder) holder).mDescription.setText(object.getDescription()); //in edittext i am using description.
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null)
            return 0;
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            CityEvent object = mList.get(position);
            if (object != null) {
                return object.getType();
            }
        }
        return 0;
    }


    public static class EventViewHolder extends RecyclerView.ViewHolder {
        private EditText mTitle;
        private EditText mDescription;

        public EventViewHolder(View itemView) {
            super(itemView);
            mTitle = (EditText) itemView.findViewById(R.id.titleTextView);
            mDescription = (EditText) itemView.findViewById(R.id.descriptionTextView);
        }
    }
}

现在的问题是,如果我更改编辑文本字段,如何让所有项目再次存储在数组列表中?

因为我需要将其作为 POST 请求参数发送到数组列表中。 那么如何让所有物品都送呢?

 //For eg: POST params:  
 "cityEvent": [
      {
       "type": "event"
       "title":"london",
       "description":"some events"
      }
      ]

主要活动:

//just pasting the main logic

DifferentRowAdapter adapter = new DifferentRowAdapter(getData());

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);
final RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(linearLayoutManager);
//this is imp
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(adapter);

我认为您需要做的是监听所有 ViewHolder 对象的每个 EditText 字段中的文本更改。您可以在 onBindViewHolder()ViewHolder class 的构造函数中设置侦听器,在您的情况下为 EventViewHolder。然后您需要 ArrayList,一个用于 title,一个用于 description,并将它们设置为您在这些字段中输入的初始值。在任何文本更改时,您可以使用 TextWatcheronTextChanged() 更新 ArrayList 中的值。当你需要传递所有的值时,只需要传递我上面提到的两个ArrayList对象即可。

这就是您的 onBindViewHolder() 在您使用 setText() 作为您的字段的行之后的样子。 titleDatadescriptionData 是两个 ArrayList<String> 对象,假定已初始化。

    titleData.add(object.getName());
    descriptionData.add(object.getDescription());

    ((EventViewHolder)holder).mTitle.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        titleData.remove(position);
        titleData.add(position, s.toString());
    }
    ((EventViewHolder)holder).mDescription.addTextChangedListener(new TextWatcher() {
    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        descriptionData.remove(position);
        descriptionData.add(position, s.toString());
    }