How to Increase or decrease value of edittext in listview's each row?

自古美人都是妖i 提交于 2019-12-20 03:12:23

问题


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

回答1:


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!




回答2:


Edit

Try declaring holder like this, and dont try to redeclare it later in the code.

final UserHolder holder = new UserHolder();

Final variables cant be reassigned




回答3:


Use final UserHolder holder; instead of UserHolder holder = new UserHolder().



来源:https://stackoverflow.com/questions/32264186/how-to-increase-or-decrease-value-of-edittext-in-listviews-each-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!