Ankit
Ankit

Reputation: 21

How to increase value in each row of a ListView separately using separate button in each row?

I have a ListView and in each list view I have a button and a TextView. On button click I want to increase te value shown in the TextView. I am able to increase the vale in the TextView but the problem is that the value is incrementing in the next row of the TextView from where it is left in previous TextView .

But I want to start from 1 for each row. How can I do that?

Here is my code

MainActivitty.java

public class MainActivity extends AppCompatActivity {


    ListView lv;
    TextView t;
int q=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    lv= (ListView) findViewById(R.id.list);
lv.setAdapter(new MyAd(this));
    }

public void hand(View v)
{
final    RelativeLayout rl= (RelativeLayout) v.getParent();
    Button b= (Button) rl.getChildAt(2);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            t= (TextView) rl.getChildAt(1);
            quantity();


        }
    });
}


public  void quantity()
{
    q=q+1;
    t.setText(""+q);
}
}

Edit 1:

My problem is solved till some extent. I am able to increment the value separately for each button click for separate rows but the value is not reflected in the text view. How to reflect it in text view?

My Code:

MainActivity.java

public class MainActivity extends AppCompatActivity  implements MyAd.customButtonListener {


    ListView lv;

int q=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    lv= (ListView) findViewById(R.id.list);
       MyAd ma=new MyAd(this);
        ma.setCustomButtonListner(this);
        lv.setAdapter(ma);
    }



    @Override
    public void onButtonClickListner(int position,TextView t) {
        q=q+1;
       t.setText(""+q);
    }
}

MyAd.java

class MyAd extends BaseAdapter {
int q=0,a[];
    List<ListItem> li=new ArrayList<>();
    Context c;
    MyViewHolder mvh;
    customButtonListener customListner;



MyAd(Context c)
{
    this.c=c;
    Resources res=c.getResources();
    String title[]=res.getStringArray(R.array.title);
a=new int[title.length];


    for(int i=0;i<title.length;i++)
    {
        li.add(new ListItem(title[i]));
    }
}







    public interface customButtonListener
    {
       void onButtonClickListner(int position, TextView t);
    }




    public void setCustomButtonListner(customButtonListener listener)
    {
        this.customListner = listener;
    }







    @Override
    public int getCount() {
        return li.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }





    @Override
    public View getView(final int position, View view, ViewGroup parent) {
View row=view;


if(row==null) {

    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.list_item, parent, false);
 mvh=new MyViewHolder();
    mvh.txt=row.findViewById(R.id.txt);
    mvh.quan=row.findViewById(R.id.quan);
    mvh.bt=row.findViewById(R.id.button);
row.setTag(mvh);
}
else
{
    mvh= (MyViewHolder) row.getTag();
}


 ListItem sr=li.get(position);
//String s=sr.title;
        mvh.txt.setText(sr.title);
        mvh.bt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       q=a[position];
        mvh.quan.setText(String.valueOf(q++));
a[position]=q;
System.out.println("position:"+position+"-"+q);
        notifyDataSetChanged();    }
});
        return row;
    }


    class MyViewHolder
    {
        TextView txt,quan;
        Button bt;
    }
}

ListItem.java

public class ListItem {

    String title;

    ListItem(String title)
    {
        this.title=title;
    }


}

Upvotes: 0

Views: 336

Answers (1)

theAndDev
theAndDev

Reputation: 672

I cannot completely tell you what is wrong with your code unless you publish your custom adapter code but as far as I can understand your problem:

  1. ListView has a onItemClickListener which gets called when you click on any item but this happens when you click on any portion of the item.
  2. In order for you to get the callback only when the button is clicked you need to set a onClickListener to the button and call an interface function inside onClick(). Inside this callback function you will write the code to change your TextView value.
  3. After changing your dataset you will call adapter.notifyDatasetChanged() in order to reflect the change in your listView.

This tutorial might help you in achieving what you want if I have understood your problem correctly.

Upvotes: 1

Related Questions