我创建了一个自定义ListView
,每行包含TextView
和EditText
。一切都很完美,但我不知道应该将TextChangeListener
设置为EditText
,以便我可以设置/保存输入的值。这是我的代码:
RowItem.java
public class RowItem {
private String title;
private String value;
public RowItem(String title, String value) {
this.title = title;
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
CustomList.java
public class CustomList extends ArrayAdapter<RowItem> {
Context context;
List<RowItem> items;
public CustomList(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
this.items = items;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
EditText txtValue;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.textView1);
holder.txtValue = (EditText) convertView.findViewById(R.id.editText1);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.txtValue.setText(rowItem.getValue());
return convertView;
}
}
有什么建议吗?
答案 0 :(得分:2)
修改viewHolder以包含它指向的行的行号。
private class ViewHolder {
TextView txtTitle;
EditText txtValue;
int rowNumber;
}
始终设置rowNumber
holder.txtValue.setText(rowItem.getValue());
holder.rowNumber = position;
在editText上添加自定义适配器作为onFocusListener,并通过以下代码获取rowNumber。
void onFocusChanged(View v, boolean hasFocus) {
ViewHolder holder = (ViewHolder)v.getParentView().getTag();
int rowNo = holder.rowNumber;
//do rest of the stuff like modifying the array list.
}
但是,我认为应该有一个比这更容易的解决方案。