我的ListView包含三个EditTexts和一个TextView,它由一个ArrayList填充。编辑文本时,我希望它更新并保存到ArrayList。我尝试按照此处链接的指南进行操作:来自https://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/的How to add EditText in listview and get its value dynamically in all the rows?,但我遇到了很多问题。
我现在得到的错误是在公共Void onFocusChange下,它说事件需要被声明为final。如果我宣布它" final"在我初始化它的EditText前面,它说"不能为最终变量事件分配值"。我模糊地理解为什么我会收到这些错误,但在指南中他们必须按原样正常工作。
这是我对ListView的自定义适配器:
public class CustomAdapter extends ArrayAdapter {
ArrayList<Event> mobileArrayList;
public CustomAdapter (Context context, ArrayList<Event> events){
super(context, 0, events);
this.mobileArrayList = events;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Event event = mobileArrayList.get(position);
ViewHolder holder = new ViewHolder();
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_row, parent, false);
holder.caption = (EditText) convertView.findViewById(R.id.events)
}
EditText events = (EditText) convertView.findViewById(R.id.events);
EditText start = (EditText) convertView.findViewById(R.id.start);
EditText end = (EditText) convertView.findViewById(R.id.end);
TextView day = (TextView) convertView.findViewById(R.id.day);
events.setText(event.getSummary());
start.setText("Start Date: " +event.getDateStart());
end.setText("End Date: " + event.getDateEnd());
day.setText(event.getDayOfTheWeek());
holder.caption.setId(position);
holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText events = (EditText) v;
mobileArrayList.get(position).setSummary(events.getText().toString());
}
}
});
return convertView;
}
class ViewHolder{
EditText caption;
}
}
我的Events类中的setSummary方法:
public void setSummary(String s){
this.summary = s;
}
我应该注意&#34;事件&#34;是我的POJO在视线下的第一行宣布和&#34;事件&#34;是一个EditText对象。我试图密切关注指南,但我没有很多经验,并且我不能100%确定它是做什么的。此外,这只适用于我的一个editTexts,我会为其他两个重复onClickListener。谢谢