gpt4 book ai didi

java - 自定义 ListView 中的增量/减量

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:44 25 4
gpt4 key购买 nike

对于我的一个项目,我想实现这个应用程序使用的那种计数器(白色的,我的是蓝色的)。我已经创建了所有内容都就位的自定义 ListView ,但由于某种原因,当我按下 + 按钮或 - 按钮时,增量不适用于该特定项目。相反,它会增加底部的项目或其他不是我按下按钮的项目。我已经尝试使用 switch case 和 onClickMethod 来获取 getView 中的特定位置,但它仍然不起作用。我的问题是如何在不影响其他计数的情况下仅增加该特定项目的计数?我正在扩展 baseAdapter 的 customAdapter 类的 getView 方法中增加和减少计数。我觉得这个问题更符合逻辑,更少的代码明智。任何帮助表示赞赏。谢谢

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

final int pos = position;
View myView = convertView;

//if this is the first time coming here
if(myInfater == null) {
myInfater = (LayoutInflater) myActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

if (myView == null) {
myView = myInfater.inflate(R.layout.row_layout, null);
}



ImageView itemImage = (ImageView) myView.findViewById(R.id.itemImage);
TextView itemName = (TextView) myView.findViewById(R.id.itemName);
TextView itemPrice = (TextView) myView.findViewById(R.id.itemPrice);

//stuff in linear layout
itemCount = (TextView) myView.findViewById(R.id.itemCount);
decreaseButton = (Button) myView.findViewById(R.id.itemDecrement);
increaseButton = (Button) myView.findViewById(R.id.itemIncrement);



final GroceryItem myItem = groceryItemList.get(position);
myItem.setCount(myCount);
myCount = myItem.getCount();
globalPos = position; //gets the position so we can use it in onClick

itemImage.setImageResource(myItem.getImageId());
itemName.setText(myItem.getName());
itemPrice.setText("$" + String.valueOf(myItem.getPrice()));
itemCount.setText(String.valueOf(myCount));


decreaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (myCount != 0) {
itemCount.setText(String.valueOf(myCount--));
System.out.println("decrease method count = " + myItem.getCount());
}
}
});

increaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemCount.setText(String.valueOf(myCount++));
//myItem.setCount(getCount() + 1);
System.out.println("count = " + myItem.getCount());

}
});

return myView;
}

@Override
public void onClick(View v) {

System.out.println("My count is " + myCount); //for debugging

switch (v.getId()) {
//the - button was pressed
case R.id.itemDecrement:
if (myCount != 0) {
System.out.println("decrement button pressed"); //for debugging
myCount --; //decrement myCount
groceryItemList.get(globalPos).setCount(myCount);
itemCount.setText(String.valueOf(myCount)); //update the count
}
break;
case R.id.itemIncrement:
System.out.println("increment button pressed"); //for debugging
myCount++; //increment myCount
groceryItemList.get(globalPos).setCount(myCount);
itemCount.setText(String.valueOf(myCount));
break;
}

}

This is my program

enter image description here

最佳答案

每个项目都需要自己的计数,首先删除 myCount 声明。在 getView 中添加此代码 fragment :

final GroceryItem myItem = groceryItemList.get(position);
//myItem.setCount(myCount);
//myCount = myItem.getCount();
//globalPos = position;
decreaseButton.setTag(myItem);
increaseButton.setTag(myItem);
itemCount.setText(String.valueOf(myItem.getCount()));

在您的onClick中使用:

GroceryItem gItem = (GroceryItem)v.getTag();
int myCount = gItem.getCount();
TextView itemCountText = v.getParent().findViewById(R.id.itemCount);
switch(v.getId())
{
case R.id.itemDecrement:
myCount = Math.max(myCount--, 0);
break;
case R.id.itemIncrement:
// Maximum quantity of 99
myCount = Math.min(myCount++, 99);
break;
}

myItem.setCount(myCount);
itemCount.setText(String.valueOf(myCount));

关于java - 自定义 ListView 中的增量/减量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31816549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com