如何从具有多个视图的ListView中的EditText获取文本? [英] How to get text from EditText within ListView with multiple Views?

查看:75
本文介绍了如何从具有多个视图的ListView中的EditText获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,每行包含两个TextViews和一个EditText.我已经搜索了并且无法找到如何将用户输入到EditText中的文本.我最近的尝试实现了一个TextWatcher.

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


}
}

推荐答案

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

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

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

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.

解决方案:

  • 定义一个listView对象,该对象标识我们关心的listview 关于
  • 定义一个可以获取列表视图的View对象
  • 定义您想要从列表视图中获取的任何字段(EditTexts,TextViews等)
  • 获取列表视图当前包含的行数
  • 使用listviewname.getChildAt(position)设置视图对象
  • 使用(EditText)viewObject.findViewById(R.id.edittextid)之类的东西来分配字段(例如,editText)
  • 使用editTextObject.getText().toString()从字段中获取值
  • 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();
}

这篇关于如何从具有多个视图的ListView中的EditText获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆