我列出了带编辑文字的视图。
我使用textchange监听器来监听edittext。它的工作性很好。
但是当列表视图滚动时,textchange监听器已经调用并且值正在改变。
如何解决这个问题?
我的样本活动如下。
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listview;
private EditorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.lst);
ArrayList<String> data = new ArrayList<String>();
for(int i=0;i<10;i++)
data.add("i="+i);
adapter = new EditorAdapter(this,data);
listview.setAdapter(adapter);
}
}
class EditorAdapter extends BaseAdapter {
Context context;
ArrayList<String> data;
public EditorAdapter(Context context, ArrayList<String> data){
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int pos) {
return data.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
holder.editPropertyValue = (EditText) convertView.findViewById(R.id.editPropertyValue);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.editPropertyValue.setTag(position);
holder.editPropertyValue.setText(data.get(position));
setNameTextChangeListener(holder, position);
convertView.setTag(holder);
return convertView;
}
class ViewHolder {
EditText editPropertyValue;
}
private void setNameTextChangeListener(final ViewHolder holder, final int position) {
holder.editPropertyValue.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0)
{
System.out.println("s.toString() "+s.toString());
data.set(position, s.toString());
Log.d("rvg", position+": "+data.toString());
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void afterTextChanged(Editable s) { }
});
}
}
答案 0 :(得分:0)
您要在TextWatcher
上添加getView()
。您应该添加TextWatcher
一次。在setNameTextChangeListener(holder, position);
中移动if(convertView==null)
方法。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = youtInflater.from(context).inflate(R.layout.row_edit_string,null,false);
holder.editPropertyValue = (EditText) onvertView.findViewById(R.id.editPropertyValue);
setNameTextChangeListener(holder, position);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.editPropertyValue.setTag(position);
holder.editPropertyValue.setText(data.get(position));
convertView.setTag(holder);
return convertView;
}