我有一个列表视图,它有一个自定义适配器,其中每一行左侧都有一个文本视图,右侧有一个编辑文本,用户可以使用它来修改条目值。
默认情况下,放置在每个视图内的 EditText 视图中的值是从传递给适配器的字符串数组中获取的。
我想知道如何允许用户编辑这些值并将结果保存回同一个字符串数组中。
我尝试添加一个文本更改侦听器,因此当用户编辑该值时,新字符串将放置在原始字符串数组中的适当位置。这样做的问题是,当用户滚动时,文本更改侦听器被激活,并且数组中的值被空白字符串覆盖。
这是一些代码:
public EditTagsListViewAdapter(Context context, String[] objectKeys, String[] objectValues) {
super();
this.context = context;
this.objectKeys = objectKeys;
this.objectValues = objectValues;
}
@Override
public int getCount() {
return objectKeys.length;
}
@Override
public String[] getItem(int position) {
String[] item = new String[2];
item[0] = objectKeys[position];
item[1] = objectValues[position];
return item;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.edit_osm_tag_row, null);
holder = new ViewHolder();
holder.key = (TextView) convertView.findViewById(R.id.tagKey);
holder.value = (EditText) convertView.findViewById(R.id.tagValue);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
String[] rowItem = (String[]) getItem(position);
holder.key.setText(rowItem[0]);
if(rowItem[1].equals("null")) {
holder.value.setText("");
} else {
holder.value.setText(rowItem[1]);
}
holder.value.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
objectValues[position] = s.toString();
Log.i(TAG, "Added changes to adapter");
}
});
return convertView;
}
static class ViewHolder {
protected TextView key;
protected EditText value;
}
我在之后将一些edittext值设置为空白,if(rowItem[1].equals("null")) {
因为变量objectValues中的一些值将被设置为字符串“null”,但我希望它们显示为空白。
希望这是有道理的。有谁知道我怎么能做到这一点?
谢谢