I know this question is repeated many times in the stackoverflow , still could not find the proper solution for this. I am building an android application in which i am using Edit text within the Listview . When I enter some data in edit text and scroll to the below part and come back, the entered data either be lost , or it will be in the different row view of list view .
I followed the below stack overflow post and written the code. Still its not working.
Edit Text in custom List View Loses Value on Scroll
Below is my Adapter code .
package com.example.listview;
import java.util.List;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
class MynewAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
int temp;
public String[] text;
//private int editingPosition = 0;
MynewAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
text= new String[list.size()];
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getview:"+position+" "+convertView);
ViewHolder holder = null;
temp=position;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater
.inflate(R.layout.row, parent, false);
holder.text = (TextView) convertView
.findViewById(R.id.label);
holder.address = (EditText) convertView
.findViewById(R.id.txtAddress);
holder.address.setTag(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//holder.address.removeTextChangedListener(watcher);
holder.address.setText(text[temp]);
/*
holder.address.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) editingPosition = temp;
}
});
*/
holder.address.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("onTextChanged size= "+list.size()+" editingposition = "+temp+" string = "+s.toString());
text[temp] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
});
holder.text.setText(list.get(position).getName());
return convertView;
}
class ViewHolder {
protected TextView text;
protected EditText address;
}
}
I have put trace in the get view method and text watcher. When the app is opened get view is called for all the visible list items and convertview will be null as nothing to recycle. And when I enter the text in the edit field of Listview item 1 , Still the position parameter will be 6. So thats the bug I found in the code. So Is there anyway to find the position of the item where the text is getting edited ? That will really solve the problem.
07-16 10:58:48.449: I/System.out(825): getview:0 null
07-16 10:58:48.469: I/System.out(825): getview:1 null
07-16 10:58:48.469: I/System.out(825): getview:2 null
07-16 10:58:48.479: I/System.out(825): getview:3 null
07-16 10:58:48.479: I/System.out(825): getview:4 null
07-16 10:58:48.489: I/System.out(825): getview:5 null
07-16 10:58:48.489: I/System.out(825): getview:6 null
07-16 10:59:33.309: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = p
07-16 10:59:54.659: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = pr
07-16 11:00:24.599: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = s
07-16 11:00:24.949: I/System.out(825): onTextChanged size= 18 editingposition = 6 string = su