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

3

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;
 }
}
Garrygarson answered 28/8, 2015 at 5:53 Comment(2)
make final UserHolder holderMeliamelic
And don't assign null during declaration, or you won't be able to assign an objectRamble
G
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!

Geosynclinal answered 28/8, 2015 at 5:56 Comment(9)
but right i have red line and shows Cannot refer to the non-final local variable holder defined in an enclosing scopeGarrygarson
near this line int mValue = Integer.parseInt(holder.textAddress.getText().toString());Garrygarson
mValue is fine, it is only defined locally. holder has to be finalRamble
Change UserHolder holder = null; to final UserHolder holder;Ramble
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 assignmentGarrygarson
try to declare mValue globallyGeosynclinal
i did..still isuue with holderGarrygarson
i have again updated my answer. try again edited answerGeosynclinal
int mValue = Integer.parseInt(tagHolder.textAddress.getText().toString()); This line still needs correction. Corrected it. Should use tagHolder in place of holder.Hipped
D
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

Dormancy answered 28/8, 2015 at 6:1 Comment(5)
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 assignmentGarrygarson
It should be final UserHolder holder;Ramble
When you use final UserHolder holder = new UserHolder(); you can't make an assignment to holder in the else partRamble
@BenjaminScharbau you are right..this one works final UserHolder holder ;...but final UserHolder holder = new UserHolder(); dint workGarrygarson
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 laterRamble
R
1

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

Ramble answered 28/8, 2015 at 6:34 Comment(3)
can you tell if i want to do same thing with pageradapter extend class?Garrygarson
Sorry, I don't really understand what you want to do with pagerAdapterRamble
in pageradapter i want to add same layout with two buttons and ediitext and want to perform same operationGarrygarson

© 2022 - 2024 — McMap. All rights reserved.