自定义列表视图中的增量/减量

时间:2015-08-04 18:23:20

标签: java android xml

对于我的一个项目,我想实现这个应用程序使用的那种计数器(白色的,我的是蓝色的)。我已经创建了自定义列表视图,但是出于某种原因,当我按下+按钮或 - 按钮时,增量不适用于该特定项目。相反,它会增加底部或其他项目的项目,而不是我按下按钮的项目。我已经尝试使用switch case和onClickMethod来获取getView中的特定位置,但它仍然无法正常工作。我的问题是如何才能为该特定项目增加而不影响其他项目?我正在扩展baseAdapter的customAdapter类的getView方法中增加和减少count。我觉得问题更合乎逻辑,代码更少。任何帮助表示赞赏。感谢

@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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

 itemCount.setText(String.valueOf(myCount--));

全局(您在评论中全局定义)定义myCount将使值减少1。 首先,您必须维护ArrayList或其他东西以维持每个项目的计数,并将其初始值设置为myItem.setCount(0);

然后当你在reduceButton监听器里面时,你必须像这样更新那个特定位置的计数值

  decreaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    // decrease value for specific position, supposes it's a list of string type .
                    myItem.set(position, myCount--);
                    itemCount.setText(String.valueOf(myItem.getCount()));

            }
        }); 

并增加特定位置的计数并更新增加按钮的监听器

  increaseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  // increase value for specific position, supposes it's a list of string type.
                   myItem.set(position, myCount++);
                   itemCount.setText(String.valueOf(myItem.getCount()));

    }
});

底线是您必须维护一个列表,然后相应地更新该位置,并且每当您想要获取这些值时,只需遍历它并从列表中获取每个值。