我一直在寻找解决问题的方法,但找不到答案,但没有一个对我有用。 :/ 我在每个列表项中都有一个带有edittext的列表视图,当向下滚动并再次向上移动时,已编辑的edittext的内容已丢失或其他edittext已占用其值。
http://i.stack.imgur.com/zWu0H.png
我有一个全局textWatcher并动态添加和删除它。 Edittexts一直在丢失内容。
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
ArrayPosition = position;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) AdapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_list_order, parent, false);
holder = new ViewHolder();
holder.FlightTimeTextView = (TextView) convertView.findViewById(R.id.itemFlightTimeTextView);
holder.BusNumberEditText = (EditText) convertView.findViewById(R.id.busNumberEditText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Order order = Orders.get(position);
holder.FlightTimeTextView.setText(order.getFlightTime());
holder.BusNumberEditText.removeTextChangedListener(textWatcher);
Log.d("String in array String", HolderBusNumbers[position]);
holder.BusNumberEditText.setText(HolderBusNumbers[position]);
holder.BusNumberEditText.addTextChangedListener(textWatcher);
return convertView;
}
int ArrayPosition = 0;
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("TextWatcher", s.toString());
HolderBusNumbers[ArrayPosition] = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
};
我正在工作一个小时,你能帮我解决我做错了什么:/ ??
答案 0 :(得分:0)
将textwatcher代码移到getView()
中public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
ArrayPosition = position;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) AdapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_list_order, parent, false);
holder = new ViewHolder();
holder.FlightTimeTextView = (TextView) convertView.findViewById(R.id.itemFlightTimeTextView);
holder.BusNumberEditText = (EditText) convertView.findViewById(R.id.busNumberEditText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Order order = Orders.get(position);
holder.FlightTimeTextView.setText(order.getFlightTime());
holder.BusNumberEditText.removeTextChangedListener(textWatcher);
Log.d("String in array String", HolderBusNumbers[position]);
holder.BusNumberEditText.setText(HolderBusNumbers[position]);
holder.BusNumberEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
Log.d("TextWatcher", s.toString());
HolderBusNumbers[ArrayPosition] = s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
答案 1 :(得分:0)
你必须使用HashMap对象,如果EditText包含或不包含值,它将存储数据。当你滚动列表视图时,它将通过调用其getView方法以及与每个edittext关联的文本再次呈现。
将HashMap全局定义为适配器
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
ArrayPosition = position;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) AdapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_list_order, parent, false);
holder = new ViewHolder();
holder.FlightTimeTextView = (TextView) convertView.findViewById(R.id.itemFlightTimeTextView);
holder.BusNumberEditText = (EditText) convertView.findViewById(R.id.busNumberEditText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Order order = Orders.get(position);
holder.FlightTimeTextView.setText(order.getFlightTime());
Log.d("String in array String", HolderBusNumbers[position]);
holder.BusNumberEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
myList.put(pos,s.toString.trim());
}
});
holder.BusNumberEditText.setText(myList.get(position));
return convertView;
}
对你有帮助!!
答案 2 :(得分:0)
我也是android的初学者,但尝试使用SimpleAdapter来显示数据
答案 3 :(得分:-1)
在静态ViewHolder中创建一个引用整数,用于保存listview中单击项的位置。然后在文本观察器中,我设置了列表的viewholder.reference位置中存在的项的字符串。 因此,静态正在帮助完成这项工作。 以下代码是有效的!
static class ViewHolder{
TextView FlightTimeTextView;
EditText BusNumberEditText;
int Reference;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) AdapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_list_order, parent, false);
holder = new ViewHolder();
holder.FlightTimeTextView = (TextView) convertView.findViewById(R.id.itemFlightTimeTextView);
holder.BusNumberEditText = (EditText) convertView.findViewById(R.id.busNumberEditText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Order order = Orders.get(position);
holder.Reference = position;
holder.FlightTimeTextView.setText(order.getFlightTime());
holder.BusNumberEditText.setText(order.getBusNumber());
holder.BusNumberEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Orders.get(holder.Reference).setBusNumber(s.toString());
}
});
return convertView;
}
}
答案 4 :(得分:-2)
将你的Textwatcher放在适配器的getView()函数中,make position(getview函数的第一个参数)final int和onTextChange执行此操作
option