How to solve scroll issue in GridView in android?

最后都变了- 提交于 2020-01-22 02:22:05

问题


I am trying to develop a simple order app.

Here I want to add/cancel the product for every child(List Item).

public class CustomAdapter extends BaseAdapter implements OnClickListener {

    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;
    public Resources res;
    ListModel tempValues = null;

    int i = 0;
    static int count = 0;
    static int tot_amt = 0;

    ImageButton btn;
    ViewHolder holder;
    int pos;

    public CustomAdapter(Activity a, ArrayList d, Resources resLocal) {

        activity = a;
        data = d;
        res = resLocal;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {

        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {

        public TextView text;
        public TextView text1;
        public TextView textWide;
        public TextView tvCounter;

        public ImageView image;
        ImageButton button;
        ImageButton decreesButton2;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        pos = position;
        if (convertView == null) {

            vi = inflater.inflate(R.layout.tabitem, null);
            btn = (ImageButton) vi.findViewById(R.id.button1);

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.text1 = (TextView) vi.findViewById(R.id.text1);
            holder.image = (ImageView) vi.findViewById(R.id.list_image);
            holder.tvCounter = (TextView) vi.findViewById(R.id.counter);
            holder.button = (ImageButton) vi.findViewById(R.id.button1);
            holder.decreesButton2 = (ImageButton) vi.findViewById(R.id.button2);

            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        if (data.size() <= 0) {
            holder.text.setText("No Data");

        } else {

            tempValues = null;
            tempValues = (ListModel) data.get(position);

            holder.text.setText(tempValues.getProductName());
            holder.text1.setText(tempValues.getPrice());
            holder.image.setScaleType(ScaleType.FIT_XY);
            byte[] outImage=tempValues.getImage();
            ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
            Bitmap theImage = BitmapFactory.decodeStream(imageStream);
            holder.image.setImageBitmap(theImage);



            /*holder.image.setImageResource(res.getIdentifier(
                    "com.list.listviewexample:drawable/"
                            + tempValues.getImage(), null, null));*/


            vi.setOnClickListener(new OnItemClickListener(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));

                    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));
                    ListViewExample sct = (ListViewExample) activity;
                    sct.getAllValues();
                }

            });
        }
        return vi;
    }

    @Override
    public void onClick(View v) {

    }

    private class OnItemClickListener implements OnClickListener {
        private int mPosition;

        OnItemClickListener(int position) {
            mPosition = position;
        }

        @Override
        public void onClick(View arg0) {

            ListViewExample sct = (ListViewExample) activity;
            sct.onItemClick(mPosition);
        }
    }
}

Now every thing is working fine. My problem is when I scroll the GridView, the total Items and total price is automatically increased and decreased. If I add 1 Pizza and if I scroll down, that Pizza total price will decreased and automatically burger item is added.

Please help. Where I did mistake. Please let me any good way to add/cancel the items in the GridView.


回答1:


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.




回答2:


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..!!



来源:https://stackoverflow.com/questions/28486148/how-to-solve-scroll-issue-in-gridview-in-android

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