如何在listview的每一行中增加或减少edittext的值? [英] How to Increase or decrease value of edittext in listview's each row?

查看:29
本文介绍了如何在listview的每一行中增加或减少edittext的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Listview,在我的 Listview 中,我有两个 Buttons 和一个 Edittext.在我的 Edittext 中,我想根据 Button 的点击增加 Edittext 的值.我学习了很多教程,但它仍然无法在我的 Listview 中工作,有人可以帮我吗?

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?

我遵循本教程:http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html

显示:不能引用封闭作用域中定义的非最终局部变量持有者

代码:

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;
 }
}

推荐答案

在这种情况下,您无法访问使用局部变量,当 onClickListener 被调用时,变量将超出范围.

You cant access use local variables in this case, By the time the onClickListener is called the variables would have gone out of scope.

因此,您也可以将 ViewHolder 设置为按钮的标签,然后您可以在 onClick 中访问它.

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 );
    }
   }
  });

希望能帮到你!

这篇关于如何在listview的每一行中增加或减少edittext的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆