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

瘦欲@ 提交于 2019-12-02 02:31:54

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!

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

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

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