简体   繁体   English

从Listview的Edittext字段中获取所有值(超过10个edittext)

[英]Getting all values from Edittext fields on Listview (More than 10 edittexts)

I have a listview with multiple edittexts on each row. 我有一个列表视图,每行上都有多个edittexts。 I would like to get the values from each Edittexts and its position so I can call them on mainactivity. 我想从每个Edittexts及其位置获取值,以便可以在mainactivity上调用它们。 I tried using hashmap and etc but I can't get any of them to work. 我尝试使用hashmap等,但是我无法使用它们。 I spent days trying to figure this out while looking through answers on stack overflow and have not solved it yet. 我花了几天的时间试图找出问题,同时查看堆栈溢出的答案,但尚未解决。 I know I should implement textwatcher and save the changes on hashmap with position. 我知道我应该实现textwatcher并将更改保存在具有位置的hashmap上。 so far, I can get the position of entire listview without a problem. 到目前为止,我可以毫无问题地获得整个listview的位置。 Only thing I'm having trouble is not losing the content of the edittexts when I scroll up and down. 唯一麻烦的是,当我上下滚动时,不会丢失edittext的内容。 Here is my code. 这是我的代码。 Code runs without a problem! 代码运行没有问题!

package com.example.jung.evergreen;


public class InteractiveArrayAdapter extends ArrayAdapter {
private int editingPosition=0;
private final List<String> list;
String text[];
private final Activity context;
public ArrayList myItems = new ArrayList();
private HashMap<String, String> textValues = new HashMap<String, String>();

public InteractiveArrayAdapter(Activity context, List<String> list) {
    super(context, R.layout.item, list);
    this.context = context;
    this.list = list;
    text=new String[list.size()];
}

static class ViewHolder {
    protected TextView text;
    protected EditText editbox;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    int temp = position;
    ViewHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater
                .inflate(R.layout.item, parent, false);

        holder.text = (TextView) convertView.findViewById(R.id.textView3);
        holder.editbox = (EditText) convertView.findViewById(R.id.editText);
        holder.editbox.setTag(position);


        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.text.setText(list.get(position));
    holder.editbox.setText(text[temp]);

    holder.editbox.removeTextChangedListener(watcher);





    holder.editbox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                editingPosition = position;
                //   final int position = v.getId();
                //    final EditText Caption = (EditText) v;
                //    String val = Caption.getText().toString();  // you have the value here
                //    Log.e("Hellooooooooooo", val);

                //    if (val.compareTo("") != 0) {
                //       String accountName = "";
                //       if (Caption.getTag() != null) {
                //           accountName = Caption.getTag().toString();  // get the tag

                //       }
                //   }

            }
        }
    });

    holder.editbox.addTextChangedListener(watcher);

    return convertView;
}


private TextWatcher watcher = new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        text[editingPosition] = s.toString();
        textValues.put(text[editingPosition], s.toString());
        Log.e("IT CHANGED", text[editingPosition] + " on " + editingPosition);
    }
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};


}

Here is the image of it running 这是它运行的图像

To keep things simple, you can identify each EditText with it's 'tag' attribute. 为了简单起见,您可以使用“ tag”属性标识每个EditText。 A Listview in android will reuse views and by setting a tag to the EditText in each 'getView', you will be able to receive it's position in the TextWatcher. android中的Listview将重用视图,并通过在每个“ getView”中将标签设置为EditText,您将能够在TextWatcher中接收其位置。

First extend your own TextWatcher, so it can hold a reference to the view: 首先扩展您自己的TextWatcher,以便它可以保存对视图的引用:

public static class CustomTextWatcher implements TextWatcher {

    private EditText mEditText;

    public MyTextWatcher(EditText editText) {
       mEditText = editText;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        final int position = Integer.valueOf(mEditText.getTag().toString())
        text[position] = s.toString();

        ...
     }
}

Then implement it in your 'getView' like this: 然后像这样在“ getView”中实现它:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ... 

    holder.text.setText(list.get(position));
    holder.editbox.setTag(position);

    holder.editbox.addTextChangedListener(new CustomTextWatcher(holder.editbox));

    ...

    return convertView;
}

Credits for the custom TextWatcher go to: How to get the View in TextWatcher method context? 自定义TextWatcher的功劳归于: 如何在TextWatcher方法上下文中获取“视图”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM