我有一个自定义视图的 listView。每个视图都包含一个editText。我正在尝试将侦听器添加到我在 getView() 中创建的 editText 变量中。问题是,当我创建侦听器时,它不允许我使用 editText 变量,除非我将其设为 final,但如果我将其设为 final,我想当该行被重用时我会遇到问题。这是我的问题的一个简单示例:
private class MyAdapter extends ArrayAdapter<Date>{
public MyAdapter(Context context, int resource, int textViewResourceId,
List<Date> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listview_cell_single, parent, false);
}else{
row = convertView;
}
EditText et = (EditText) row.findViewById(R.id.editText_single);
et.setText("" + position);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
et.setText("You edited row: " + position);
//Error: et must be final, position must be final
}
}
});