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

时间:2018-01-11 17:11:55

标签: java android arrays android-recyclerview android-json

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

但名称和描述有edittext,用户可以在其中更改名称,描述并提交整个字段。

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

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);
        }
    }
}

所以我将它放在edittext字段中。

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

现在的问题是,我是否更改了edittext字段,如何再次将所有项目存储到数组列表中?

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

//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);

MainActivity:

$str = "This is Agbeniga , he is a very good boy. He?s loved , but is he really is ? No , i don?t think so. but it?s working very good anyway , ya?ll all darling. Okay?";
$str= preg_replace('/\b\?\b/',"'",$str);
echo  $str;

1 个答案:

答案 0 :(得分:1)

我认为您需要做的是在所有EditText对象的每个ViewHolder字段中监听文本更改。您可以在onBindViewHolder()ViewHolder类的构造函数中设置侦听器,在您的情况EventViewHolder中。然后,您需要ArrayList,一个用于title,一个用于description,并使用您在这些字段中输入的初始值设置它们。对于任何文字更改,您可以使用ArrayList&#39; s TextWatcher更新onTextChanged()中的值。当您需要传递所有值时,您只需要传递我上面提到的两个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());
    }