如何在android中的GridView中解决滚动问题?(How to solve scroll issue in GridView in android?)

编程入门 行业动态 更新时间:2024-10-23 06:30:28
如何在android中的GridView中解决滚动问题?(How to solve scroll issue in GridView in android?)

我正在尝试开发一个简单的订单应用程序。

在这里,我想为每个孩子添加/取消产品(列表项)。

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); } } }

现在每件事都很好。 我的问题是当我滚动GridView时,总项目和总价格会自动增加和减少。 如果我添加1个比萨并且如果我向下滚动,那么比萨饼的总价格将会降低并且会添加自动汉堡项目。

请帮忙。 我错了。 请允许我在GridView中添加/取消项目的任何好方法。

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.

最满意答案

ListView / GridView重用视图。 在你的情况下,这意味着当你有1个比萨时,你向下滚动以便比萨消失,列表视图会重新使用该视图以显示下一个视图,因此你将自动拥有一个项目:1披萨的观点。

您需要的是存储每个项目的计数并在getView中相应地设置tvCounter。

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.

更多推荐

本文发布于:2023-07-04 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026894.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   android   GridView   issue   scroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!