【发布时间】:2015-08-28 05:53:26
【问题描述】:
我创建了一个Listview,在我的Listview 中我有两个Buttons 和一个Edittext。在我的Edittext 中,我想根据Button 的点击增加Edittext 的值。我学习了很多教程,但在我的Listview 中仍然无法使用,谁能帮我解决这个问题?
我遵循本教程: http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html
显示:Cannot refer to the non-final local variable holder defined in an enclosing scope
代码:
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.textAddress = (EditText) row.findViewById(R.id.textView2);
holder.textLocation = (TextView) row.findViewById(R.id.textView3);
holder.btnEdit = (Button) row.findViewById(R.id.button1);
holder.btnDelete = (Button) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.textName.setText(user.getName());
//holder.textAddress.setText(user.getAddress());
holder.textLocation.setText(user.getLocation());
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
/* Toast.makeText(context, "Edit button Clicked",
Toast.LENGTH_LONG).show();*/
int mValue = Integer.parseInt(holder.textAddress.getText().toString());
mValue--;
if(mValue < 0)
{
System.out.println("not valid");
}
else
{
holder.textAddress.setText( ""+mValue );
}
}
});
holder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Delete Button Clicked", "**********");
/* Toast.makeText(context, "Delete button Clicked",
Toast.LENGTH_LONG).show();*/
}
});
return row;
}
static class UserHolder {
TextView textName;
EditText textAddress;
TextView textLocation;
Button btnEdit;
Button btnDelete;
}
}
【问题讨论】:
-
制作
final UserHolder holder -
并且声明时不要赋值null,否则无法赋值对象
标签: android android-listview android-viewholder