0

I have a listview with multiple edittexts on each row. I would like to get the values from each Edittexts and its position so I can call them on mainactivity. I tried using hashmap and etc but I can't get any of them to work. 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. so far, I can get the position of entire listview without a problem. Only thing I'm having trouble is not losing the content of the edittexts when I scroll up and down. 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

  • Just an idea. You can simply use this: Name ur edit texts like something 1, something 2, etc. And then using a while statement and ur positions to match the id of the edit texts u can save it to list from where u can get the values that you want. – KISHORE_ZE Dec 28 '15 at 21:04
  • this is where an array comes in handy. when you set the edit text (text) you set the array text (item) at that same position in the array as its the same as the listview item pos. Then all you do is read the array when you want the values, also you set the text in the edittext using position to insert the text as you scroll up and down so you dont loose the text. – Tasos Dec 28 '15 at 21:09

1 Answers1

0

To keep things simple, you can identify each EditText with it's 'tag' attribute. 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.

First extend your own TextWatcher, so it can hold a reference to the view:

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:

@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?

Community
  • 1
  • 1