0

We are having a spring web application for our canteen. I am trying to develop an android app in that users will be able to see all the items and put their quantity in front of item name using editText.

I am able to get response as List using REST to get all the items and show to listView. But I find difficulties to put editText in front of all items in listView because number of items are dynamic.

How to create dynamic editText and put them in a listView along with item names received using REST and stored in a List

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
ashok
  • 51
  • 2
  • 10
  • the fact you got the list from REST or not doesnt really matter, removed the REST tag from OQ. – Ofek Ron Jun 08 '16 at 08:33
  • 1
    I don't see the problem, logically you just need to create a listadapter with items size you are getting from the rest command. you can still use a listview as usual – visionix visionix Jun 08 '16 at 08:46

2 Answers2

1

As Ofek stated in his answer the most appropriate way to do this is with recyclerView and a custom adapter.

First you need a layout for a single item in your list like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="60dp"
        android:id="@+id/item_edittext"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/item_label"/>
</LinearLayout>

Next you have to create the adapter which handles the data and also listens for changes of text and focus:

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {

public static final String TAG = "CustomAdapter";
private List<String> labels;

public CustomAdapter(List<String> labels) {
    this.labels = labels;
}

@Override
public int getItemCount() {
    return labels.size();
}

@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
    customViewHolder.label.setText(this.labels.get(i));
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    //inflate layout and create new ViewHolder
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.custom_item_layout, viewGroup, false);

    return new CustomViewHolder(itemView);
}

//the viewHolder handles the single items in recylerview
public static class CustomViewHolder extends RecyclerView.ViewHolder {

    protected TextView label;
    protected EditText editText;

    public CustomViewHolder(View v) {
        super(v);
        label =  (TextView) v.findViewById(R.id.item_label);
        editText = (EditText)  v.findViewById(R.id.item_edittext);

        //listen for changes in your editText
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //listen for focusChangeEvents
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override

            //at this point may save changed data
            public void onFocusChange(View v, boolean hasFocus) {
                if(! hasFocus) {
                    Log.d(TAG, getAdapterPosition()+" has lost focus ");
                }
            }
        });
    }
}
}

In your activity use it like this:

ArrayList<String> labels = new ArrayList<>();
labels.add("label1");
labels.add("label2");
labels.add("label3");
labels.add("label4");
labels.add("label5");


RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
CustomAdapter customAdapter = new CustomAdapter(labels);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true)
arnoduebel
  • 339
  • 1
  • 9
0

ListView with EditTexts is bad combination, In my case i had to migrate to dynamically filled vertical LinearLayout in conjuction with ScrollView so that the EditTexts behave and they really did. If the size of your List is not too big and your views are not too complicated you should do the same. Otherwise, i would try migrating to RecyclerView and see how EditTexts would behave then, i cant tell if they would as i havent tried doing so yet.

see ScrollView+LinearLayout xml

and see add Views to LinearLayout programatically

and Inflating views from xml

Community
  • 1
  • 1
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • Hi Ofek Ron, I am very new in android development so please give me some idea about LinearLayout with example if possible. – ashok Jun 08 '16 at 08:23