我创建一个列表视图
,在我的列表视图
我有两个按钮
和一个 EDITTEXT
。在我的 EDITTEXT
我想增加 EDITTEXT
的值按按钮
的点击。我也跟着这么多的教程,但它仍然不是我的列表视图
工作的任何人都可以帮我吗?
我按照这个教程: http://www.androidhub4you.com/2013/02/muftitouch -listview-多click.html
这说明:无法指在一个封闭的范围内定义的非最终的局部变量持有
code:
公共类UserCustomAdapter扩展ArrayAdapter<使用者> {
上下文语境;
INT layoutResourceId;
ArrayList的<使用者>数据=新的ArrayList<使用者>();
公共UserCustomAdapter(上下文的背景下,INT layoutResourceId,
ArrayList的<使用者>数据) {
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看排= convertView;
UserHolder支架=无效;
如果(行== NULL){
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假);
持有人=新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 =(按钮)row.findViewById(R.id.button1);
holder.btnDelete =(按钮)row.findViewById(R.id.button2);
row.setTag(保持器);
} 其他 {
支架=(UserHolder)row.getTag();
}
用户USER = data.get(位置);
holder.textName.setText(user.getName());
//holder.textAddress.setText(user.getAddress());
holder.textLocation.setText(user.getLocation());
holder.btnEdit.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
Log.i(编辑单击按钮,**********);
/ * Toast.makeText(背景下,编辑单击按钮,
Toast.LENGTH_LONG).show(); * /
INT mValue =的Integer.parseInt(holder.textAddress.getText()的toString());
mValue--;
如果(mValue℃,)
{
的System.out.println(无效);
}
其他
{
holder.textAddress.setText(+ mValue);
}
}
});
holder.btnDelete.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
Log.i(删除单击按钮,**********);
/ * Toast.makeText(背景下,删除单击按钮,
Toast.LENGTH_LONG).show(); * /
}
});
返回行;
}
静态类UserHolder {
TextView的textName;
的EditText textAddress;
TextView的textLocation;
按钮btnEdit;
按钮btnDelete;
}
}
解决方案
在这种情况下,你不能访问使用局部变量,由当时的onClickListener称为变量会超出范围。
因此,你可以设置ViewHolder作为按钮标签太多,那么你就可以访问你的onClick。
holder.btnEdit.setTag(架);
holder.btnEdit.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
ViewHolder tagHolder =(ViewHolder)v.getTag();
// TODO自动生成方法存根
Log.i(编辑单击按钮,**********);
/ * Toast.makeText(背景下,编辑单击按钮,
Toast.LENGTH_LONG).show(); * /
INT mValue =的Integer.parseInt(tagHolder.textAddress.getText()的toString());
mValue--;
如果(mValue℃,)
{
的System.out.println(无效);
}
其他
{
tagHolder.textAddress.setText(+ mValue);
}
}
});
我希望它能帮助!
I create one Listview
, in my Listview
I have two Buttons
and one Edittext
. In my Edittext
I want to increase the value of Edittext
as per Button
's click. I followed so many tutorials, but still it's not working in my Listview
can anyone help me with that?
I follow this tutorial: http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html
It shows: Cannot refer to the non-final local variable holder defined in an enclosing scope
Code:
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;
}
}
解决方案
You cant access use local variables in this case, By the time the onClickListener is called the variables would have gone out of scope.
So instead you can set the ViewHolder as a tag for the button too, then you can access that in your 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 );
}
}
});
I hope it helps!
相关推荐
最新文章