如何从ListView中的EditText获取具有多个视图的文本?

时间:2015-07-02 02:05:58

标签: android android-listview android-edittext

我有一个ListView,每行包含两个TextView和一个EditText。我搜索并搜索过,无法弄清楚如何获取用户输入EditText的文本。我最近的尝试实现了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;
    }


}
}

2 个答案:

答案 0 :(得分:5)

对于那些可能有类似问题的人,我解决了我遇到的问题。

问题: 需要获取在listView中包含的多个editTexts中输入的值。使用listviewname.getItemAtPosition(整数)只获取第一个editText的值。此外,我在listview的每一行中都包含多个视图,因此getItemAtPosition仅返回listView第一行中第一个视图的值。

解决方案:

  • 定义一个listView对象,用于标识我们关注的列表视图 约
  • 定义可以获取listview的View对象
  • 定义您要从列表视图中获取的任何字段(EditTexts,TextViews等)
  • 获取listview当前包含的行数
  • 使用listviewname.getChildAt(position)
  • 设置视图对象
  • 使用类似(EditText)的方式分配字段(例如,editText)viewObject.findViewById(R.id.edittextid)
  • 使用editTextObject.getText()从字段中获取值.toString()

这里有一些可以在按下按钮时实现的示例代码:

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

答案 1 :(得分:0)

getFocusedChild(position)它将返回一个View或findFocus()

View row = ingredient_list.getFocusedChild();
EditText et =  (EditText)row.findViewById(R.id.ingredient_qty_in_new_meal_editText);

编辑

对不起,我误解了创建一个要用输入法吐司的要点

https://gist.github.com/imjarp/9313c687ccbe339d052f