Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text from EditText within ListView with multiple Views?

I have a ListView that contains two TextViews and one EditText in each row. I have searched and searched and cannot figure out how to get the text that the user enters into the EditText. My most recent attempt implements a TextWatcher.

public void addIngredientToMeal(String ingredientName) {

    ingredient_list = (ListView) findViewById(R.id.ingredients_in_meal_listView);

    int numberOfIngredients = ingredient_list.getChildCount();

    ListAdapter adapter = new ListAdapter();
    adapter.name_array = new String[numberOfIngredients+1];

    for (int i = 0; i < numberOfIngredients; i++)
        adapter.name_array[i] = (String) ingredient_list.getItemAtPosition(i);

    adapter.name_array[numberOfIngredients] = ingredientName;
    ingredient_list.setItemsCanFocus(true);
    ingredient_list.setAdapter(adapter);
}

private class ListAdapter extends BaseAdapter{

    public String[] name_array, qty_array;

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(name_array != null && name_array.length != 0){
            return name_array.length;    
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return name_array[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            //ViewHolder holder = null;

            final ViewHolder holder;
                if (convertView == null) {

                    holder = new ViewHolder();
                    LayoutInflater inflater = NewMeal.this.getLayoutInflater();
                    convertView = inflater.inflate(R.layout.ingredients_in_meal_layout, null);
                    holder.textView1 = (TextView) convertView.findViewById(R.id.ingredient_name_in_new_meal_textView);
                    holder.editText1 = (EditText) convertView.findViewById(R.id.ingredient_qty_in_new_meal_editText);
                    holder.textView2 = (TextView) convertView.findViewById(R.id.ingredient_unit_in_new_meal_textView);

                    convertView.setTag(holder);

                } else {

                    holder = (ViewHolder) convertView.getTag();
                }

                holder.ref = position;
                holder.textView1.setText(name_array[position]);
                holder.textView2.setText(databaseAdapter.dbQuery(name_array[position]));
                holder.editText1.setText(qty_array[position]);
                holder.editText1.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                            // TODO Auto-generated method stub
                            }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                    int arg3) {
                                // TODO Auto-generated method stub

                        }

                        @Override
                        public void afterTextChanged(Editable arg0) {
                                qty_array[holder.ref] = arg0.toString();

                        }
                });
            return convertView;
    }

    private class ViewHolder {
        TextView textView1, textView2;
        EditText editText1;
        int ref;
    }


}
}
like image 584
Luke Stuemke Avatar asked Jul 02 '15 02:07

Luke Stuemke


1 Answers

For those who may have a similar issue, I solved the problem I was having.

The problem: Need to get values entered in multiple editTexts that are contained within a listView. Using listviewname.getItemAtPosition(integer) only gets the value of the first editText. Moreover, I had multiple views contained in each row of the listview, so getItemAtPosition only returned the value of the first view in the first row of the listView.

The solution:

  • Define a listView object that identifies the listview that we care about
  • Define a View object that can get the listview
  • Define whatever field(s) you are trying to get from the listview (EditTexts, TextViews Etc.)
  • Get the number of rows that the listview currently contains
  • Set the view object using listviewname.getChildAt(position)
  • Assign the field (say, an editText) using something like (EditText) viewObject.findViewById(R.id.edittextid)
  • Get the value from the field using editTextObject.getText().toString()

Here's some sample code that could be implemented on a button press:

ListView lvName = (ListView) findViewById(R.id.listview1);
View v;
EditText et;

int listLength = lv.getChildCount();
String[] valueOfEditText = new String(listLength);
for (int i = 0; i < listLength; i++)
{
    v = lvName.getChildAt(i);
    et = (EditText) v.findViewById(R.id.edittext1);
    valueOfEditText[i] = et.getText().toString();
}
like image 116
Luke Stuemke Avatar answered Sep 29 '22 06:09

Luke Stuemke