如何在每行中获取listview项目点击次数?

时间:2015-04-28 04:45:36

标签: android listview android-listview

我有一个ListView,其中每个listview项目中都有一个按钮。当用户点击每行中的按钮时,我想单独获取每行中按钮的点击次数当我有一个计数器,当用户点击时,该计数器会递增按钮。但是每一行的计数都不是单独的。只要点击任何按钮,它就会增加。无论行是什么。

显然每个listview行都有一个添加按钮。单击该按钮时,我想用点击次数更改每个按钮标签。

代码:

list = getListView();
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        final int position, long id) {
                    // TODO Auto-generated method stub



                    TextView add = (TextView) view
                                .findViewById(R.id.btn_add);
                        DishCount = (TextView) view
                                .findViewById(R.id.textDishCount);


                        ((TextView) view
                        .findViewById(R.id.btn_add)).setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) 
                            {
                              try
                                {
                                    counter.add(position,counter.get(position)+1);
                                    }

                                catch (Exception e)
                                {
                                    e.printStackTrace(); 
                                    counter.add(position, 1); 
                                    }


                                totalcount= counter.get(position);
                                Toast.makeText(getApplicationContext(),"Value is "+totalcount, Toast.LENGTH_SHORT).show();

                                DishCount.setVisibility(View.VISIBLE);
                                DishCount.setText(String.valueOf(totalcount));



                            }
                        });

                    }


            });

更新方法:

private void updateView(int index){

            View v = list.getChildAt(index - 
                list.getFirstVisiblePosition());

            if(v == null)
               return;

            count++;
            TextView someText = (TextView) v.findViewById(R.id.textDishCount);
            someText.setVisibility(View.VISIBLE);

            someText.setText(String.valueOf(count));
        }

适配器类:

public class RestaurantSubMenuListAdapter extends ArrayAdapter<String> {

private final Activity context;

private final ArrayList<Bitmap> dishImagesRounded;
private final String[] DishName;
private final String[] DishDescription;
private final String[] DishPrice;
Integer count;
TextView itemCount;
Typeface tf,tfb;

public RestaurantSubMenuListAdapter(Activity context,ArrayList<Bitmap> dishImagesBitmapArray, String[] dishNameArray,
        String[] dishDescriptionArray, String[] dishPriceArray, Typeface tf, Typeface tfb) 
{
    super(context, R.layout.restaurant_menu_list_item,dishNameArray);

    this.context=context;
    this.dishImagesRounded=dishImagesBitmapArray;
    this.DishName=dishNameArray;
    this.DishDescription=dishDescriptionArray;
    this.DishPrice=dishPriceArray;
    this.tf= tf;
    this.tfb=tfb;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = convertView;
final ViewHolder h;
if(v == null){
    v=inflater.inflate(R.layout.restaurant_menu_list_item, parent,false);
    h = new ViewHolder();
    h.DishName = (TextView)v.findViewById(R.id.textDishName);
    h.DishDescription = (TextView)v.findViewById(R.id.textDishDescription);
    h.DishPrice = (TextView)v.findViewById(R.id.textDishPrice);
    h.AddButton = (TextView)v.findViewById(R.id.btn_add);

    h.ImageDish = (ImageView) v.findViewById(R.id.imageDish);
    h.DishCount = (TextView) v.findViewById(R.id.textDishCount);
    h.AddButton = (TextView) v.findViewById(R.id.btn_add);



    v.setTag(h);
}else
{
   h = (ViewHolder) v.getTag();
}

h.DishName.setTypeface(tfb);
h.DishDescription.setTypeface(tf);
h.DishPrice.setTypeface(tfb);
h.AddButton.setTypeface(tf);
h.DishCount.setTypeface(tf);
h.DishName.setText(DishName[position]);
h.DishDescription.setText(DishDescription[position]);
h.DishPrice.setText("$"+DishPrice[position]);
h.ImageDish.setImageBitmap(this.dishImagesRounded.get(position));

h.ImageDish.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent myIntent = new Intent(context,
                ActivitySelectIngredients.class);
        myIntent.putExtra("TableNumber",23);
        context.startActivity(myIntent);


    }
});

/*h.AddButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        String tag = (String)v.getTag();

        Toast.makeText(context, tag, Toast.LENGTH_SHORT).show();
        h.DishCount.setVisibility(View.VISIBLE);
        notifyDataSetChanged();

        //count++;



    }
});*/

return v;
} 

/** 
 * to stop recycling of listview items
 */
@Override
public int getViewTypeCount() {
    return getCount();
}

/** 
 * to stop recycling of listview items
 */
@Override
public int getItemViewType(int position) {
    return position;
}

private static class ViewHolder{
TextView DishName,DishDescription,DishPrice,AddButton,DishCount;
ImageView ImageDish;

}

4 个答案:

答案 0 :(得分:0)

您需要制作另一个列表或数组并保存计数器。所有行都不能有一个计数器

答案 1 :(得分:0)

您可以使用自定义类型的静态ArrayList,并使用它在各自的位置存储点击次数。

以下是您可以使用的对象代码。

public class ItemCount {
    int position;
    int count;
}

答案 2 :(得分:0)

  1. 添加数据结构:数组或arraylist:

    ArrayList<Integer> count=new ArrayList<Integer>();

  2. 在onClickLister中的List中添加Count:list.setOnItemClickListener

    ArrayList<Integer> count = new ArrayList<Integer>();
    try {
        count.add(position, count.get(position) + 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
  3. 获取位置n的项目总数:

    int totalcount= count.get(n);

答案 3 :(得分:0)

只需创建另一个arrayList并增加相同位置的计数。