2

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;
 }
}
daemmie
  • 6,361
  • 3
  • 29
  • 45
Aditya
  • 1,508
  • 1
  • 19
  • 37

3 Answers3

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!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
1

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

Oskari Mantere
  • 192
  • 1
  • 6
  • final UserHolder holder = null; i did this way...now it shows red line near holder = new UserHolder(); and says The final local variable holder cannot be assigned. It must be blank and not using a compound assignment – Aditya Aug 28 '15 at 06:07
  • 1
    It should be ```final UserHolder holder;``` – Benjamin Scharbau Aug 28 '15 at 06:10
  • When you use ```final UserHolder holder = new UserHolder();``` you can't make an assignment to holder in the `else` part – Benjamin Scharbau Aug 28 '15 at 06:17
  • @BenjaminScharbau you are right..this one works final UserHolder holder ;...but final UserHolder holder = new UserHolder(); dint work – Aditya Aug 28 '15 at 06:18
  • When you assign a value to a final variable, you can't change it. That's why you should only declare the final variable first, and then add the value later – Benjamin Scharbau Aug 28 '15 at 06:19
1

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

Benjamin Scharbau
  • 2,028
  • 1
  • 16
  • 33