我在Listview
中创建了一个Listview
我有两个Buttons
和一个Edittext
。在我的Edittext
中,我希望根据Edittext
点击增加Button
的值。我按照了很多教程,但仍然无法在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;
}
}
答案 0 :(得分:1)
在这种情况下,您无法访问使用局部变量,在调用onClickListener时,变量将超出范围。
因此您可以将ViewHolder设置为按钮的标记,然后您可以在onClick中访问它。
holder.btnEdit.setTag(holder);
holder.btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewHolder tagHolder = (ViewHolder) v.getTag();
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
/* Toast.makeText(context, "Edit button Clicked",
Toast.LENGTH_LONG).show();*/
int mValue = Integer.parseInt(tagHolder.textAddress.getText().toString());
mValue--;
if(mValue < 0)
{
System.out.println("not valid");
}
else
{
tagHolder.textAddress.setText( ""+mValue );
}
}
});
我希望它有所帮助!
答案 1 :(得分:1)
修改强>
尝试声明像这样的持有者,不要试图在代码中重新声明它。
final UserHolder holder = new UserHolder();
最终变量无法重新分配
答案 2 :(得分:1)
使用final UserHolder holder;
代替UserHolder holder = new UserHolder()
。