-1

I am using name and description in RecyclerView.

But name and description has edittext where user can change the name,description and submit the entire fields.

Now question is how to get the entire fields of all items in the 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;
    }
}

So I placed this in edittext fields.

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

Now question is if i change the edittext fields how to get all items once again to store in array list?

Because i need to send this in array list as POST request parameters. So how to get all items to send it?

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

MainActivity:

//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);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Shiv
  • 129
  • 3
  • 12
  • There is no such functionality as you want. To solve you task you have to set OnFocusChangeListener to your edit text and set new values to your objects after edittext lose focus. – Eugene Troyanskii Jan 11 '18 at 17:25

1 Answers1

1

I think what you need to do is listen for text change in each of your EditText fields for all ViewHolder objects. You can set the listeners either in onBindViewHolder() or in the constructor of ViewHolder class, in your case EventViewHolder. Then you need to have to ArrayList, one for title and one for description and set them with the initial values that you put into these fields. On any text change, you can update the value in the ArrayList by using TextWatcher's onTextChanged(). When you need to pass all the values, you just need to pass the two ArrayList objects I mentioned above.

This is how your onBindViewHolder() should look like after the line where you have used setText() for your fields. titleData and descriptionData are two ArrayList<String> objects and assumed to be initialized.

    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());
    }
Pulak
  • 768
  • 7
  • 16