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