How to solve scroll issue in GridView in android?

前端 未结 2 1184
礼貌的吻别
礼貌的吻别 2021-01-28 01:00

I am trying to develop a simple order app.

\"This

Here I want to add/cancel the product

相关标签:
2条回答
  • 2021-01-28 01:14

    ListView/GridView reuses views. What this means in your case is that when e.g. you have 1 Pizza, you scroll down so the Pizza disappears, the listview reuses that view for the next view that appears, so you'll automatically have a Items: 1 that was left over from the Pizza's view.

    What you need to to is store the count for every item and set tvCounter accordingly in getView.

    0 讨论(0)
  • 2021-01-28 01:14

    So, Finally I found the solution for scroll issue..

    vi.setOnClickListener(new OnItemClickListener(position));
                final int pos = position;
                holder.button.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        View parentView = (View) v.getParent();
    
                        int totAmount = Integer.parseInt(((TextView) parentView
                                .findViewById(R.id.text1)).getText()
                                .toString());
    
                        count = Integer.parseInt(((TextView) parentView
                                .findViewById(R.id.counter)).getText().toString());
                        count++;
                        tot_amt = totAmount * count;
    
                        ((TextView) parentView.findViewById(R.id.counter))
                                .setText(String.valueOf(count));
    
                        ((TextView) parentView.findViewById(R.id.totalPrice))
                                .setText(String.valueOf(tot_amt));
    
                        //Here I am saving the total amount and quantity
                        tempValues = null;
                        tempValues = (ListModel) data.get(pos);
                        tempValues.setProductName(tempValues.getProductName());
                        tempValues.setPrice(tempValues.getPrice());
                        tempValues.setImage(tempValues.getImage());
                        tempValues.setQuantity(String.valueOf(count));
                        tempValues.setTotalAmount(String.valueOf(tot_amt));
                        data.set(pos, tempValues);
                        ListViewExample sct = (ListViewExample) activity;
                        sct.getAllValues();
                    }
    
                });
    
                holder.decreesButton2.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        View parentView = (View) v.getParent();
                        int totAmount = Integer.parseInt(((TextView) parentView
                                .findViewById(R.id.text1)).getText()
                                .toString());
    
                        count = Integer.parseInt(((TextView) parentView
                                .findViewById(R.id.counter)).getText().toString());
                        if (count > 0) {
                            count--;
    
                            tot_amt = totAmount * count;
    
                            ((TextView) parentView.findViewById(R.id.totalPrice))
                                    .setText(String.valueOf(tot_amt));
                            ((TextView) parentView.findViewById(R.id.counter))
                                    .setText(String.valueOf(count));
    
                        //Here I am saving the total amount and quantity
                            tempValues = null;
                            tempValues = (ListModel) data.get(pos);
                            tempValues.setProductName(tempValues
                                    .getProductName());
                            tempValues.setPrice(tempValues.getPrice());
                            tempValues.setImage(tempValues.getImage());
                            tempValues.setQuantity(String.valueOf(count));
                            tempValues.setTotalAmount(String.valueOf(tot_amt));
                            data.set(pos, tempValues);
                            ListViewExample sct = (ListViewExample) activity;
                            sct.getAllValues();
                        }
                    }
    
                });
    

    Thanks Longi..!!

    0 讨论(0)
提交回复
热议问题