我正在尝试开发一个简单的订单应用。
这里我想为每个孩子添加/取消产品(列表项)。
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中添加/取消项目的任何好方法。
答案 0 :(得分:0)
ListView / GridView重用视图。在您的情况下,这意味着什么时候,例如你有1个比萨饼,你向下滚动以便比萨饼消失,列表视图会重复使用该视图以显示下一个视图,因此您将自动拥有从比萨饼视图中遗留下来的项目:1
您需要存储每个项目的计数,并在getView中相应地设置tvCounter。
答案 1 :(得分:0)
所以,最后我找到了滚动问题的解决方案..
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();
}
}
});
感谢Longi .. !!