0

我正在开发一个 android 应用程序,我必须在其中使用带有复选框的自定义列表视图。当用户选中/取消选中时,我需要分别在数组列表中添加/删除对象。

这是我的代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    selectedView=view;
    System.out.println("Position of list item:"+position);
    if(parent.getAdapter().equals(categoryAdapter))
    {       
        System.out.println("First Adapter");        
        fl=new FoodList();      
        System.out.println("Tag:"+view.getTag());
        btOrder.setVisibility(View.GONE);
        addToOrder.setVisibility(View.VISIBLE);
        back.setVisibility(View.VISIBLE);
        buttonSeperator.setVisibility(View.VISIBLE);


        fl.bundleCategory=fc.list[position];        
        fl.foodListService();       
        checks=new SparseBooleanArray();        
        quantity=new SparseArray<Integer>();

        orderList=new ArrayList<Order>();
        order=new Order();
        Order Null=null;
        for(int i=0;i<fl.foodList.size();i++)
        {
            checks.put(i, false);
            quantity.put(i, 0);
            order.setQuantity(0);
            order.setItem("");
            //orderList.add(i,Null);
            //orderList.add(i,order);
            orderList.add(order);
            System.out.println("Adding at pos:"+i+"Value:"+Null);
        }   
        WaiterFoodAdapter foodAdapter=new WaiterFoodAdapter(this, R.layout.take_order_list, fl.foodList);
        categoryList.setAdapter(foodAdapter);
        categoryList.setOnItemClickListener(this);
        }
        else
        {           
            System.out.println("Second Adapter:Id="+parent.getId());

            if(view != null)
            {
                final TextView tvItem=(TextView)selectedView.findViewById(R.id.foodName);   
                final TextView tvQuantity=(TextView)selectedView.findViewById(R.id.quantity);

                selectedPosition=position;
                SelectFood = (CheckBox)view.findViewById(R.id.selectFood);

                if(SelectFood.isChecked())          
                {           
                    SelectFood.setChecked(false);                   
                    checks.put(selectedPosition, false);
                    quantity.put(selectedPosition,0);

                    //order.setQuantity(0);
                    //order.setItem("");
                    System.out.println("Removing at pos:"+selectedPosition+"Value:"+orderList.get(selectedPosition).getItem()+orderList.get(selectedPosition).getQuantity());
                    orderList.remove(orderList.get(selectedPosition));

                    tvQuantity.setText("0");
                    System.out.println("Position="+selectedPosition);
                }           
                else
                {
                    SelectFood.setChecked(true);                    
                    checks.put(selectedPosition, true);
                    System.out.println("Position="+selectedPosition);

                    //orderList=new ArrayList<Order>();                     
                    final AlertDialog.Builder quantityAlert= new AlertDialog.Builder(TakeOrder.this);
                    quantityAlert.setTitle("Quantity");
                    quantityAlert.setMessage("Please enter quantity");

                    final EditText input = new EditText(TakeOrder.this);
                    input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    quantityAlert.setView(input);

                    quantityAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                    {   
                        public void onClick(DialogInterface dialog, int whichButton)
                        {
                            value = input.getText();



                            if(value.toString().equals(""))
                            {               
                                SelectFood.setChecked(false);
                                Toast msg=Toast.makeText(TakeOrder.this, "Please provide quantity", Toast.LENGTH_SHORT);
                                msg.show();       
                            }
                            else
                                if(value.toString().equals("0"))
                                {                       
                                    SelectFood.setChecked(false);                        
                                    Toast msg=Toast.makeText(TakeOrder.this, "Please enter valid quantity", Toast.LENGTH_SHORT);                          
                                    msg.show();
                                }
                                else                    
                                {                                   
                                    tvQuantity.setText(value);
                                    quantity.put(selectedPosition, Integer.valueOf(value.toString()));
                                    order.setQuantity(Integer.valueOf(tvQuantity.getText().toString()));
                                    order.setItem(tvItem.getText().toString());
                                    orderList.add(selectedPosition, order);
                                    System.out.println("setting at pos:"+selectedPosition+"Value:"+orderList.get(selectedPosition).getItem()+orderList.get(selectedPosition).getQuantity());
                                }



                            //Log.v("Item:",orderList.get(selectedPosition).getItem());
                            //Log.v("Quantity:",String.valueOf(orderList.get(selectedPosition).getQuantity()));

                        }               
                    });

                quantityAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                      public void onClick(DialogInterface dialog, int whichButton)
                      {
                          SelectFood.setChecked(false);                
                      }
                    });

                quantityAlert.show();
            }                                           
        }

    }

}

当用户单击 addToOrder 按钮时,它只搜索选中的复选框并获取选中的 listItems 的相关 textViews 值。

public void addToOrder(View v)
{
    for(int i=0;i<fl.foodList.size();i++)
    {           
        if(checks.get(i)==true)
            {
            System.out.println("Order is:"+orderList.get(i).getItem()+orderList.get(i).getQuantity());
            }                   
    }
}

这是我的适配器,仅用于自定义视图目的

public class WaiterFoodAdapter extends ArrayAdapter<Food> 
{
    Context context;
    TakeOrder holder;
    int layoutResourceId;
    List<Food> foodList;

    public WaiterFoodAdapter(Context context, int layoutResourceId,List<Food> foodList)
    {
        super(context, layoutResourceId, foodList);         
        this.context=context;
        this.layoutResourceId=layoutResourceId;
        this.foodList=foodList;
    }

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

        if(row==null)
        {
            LayoutInflater inflater=((Activity)context).getLayoutInflater();
            row=inflater.inflate(layoutResourceId, parent,false);
            holder=new TakeOrder();
            holder.foodIcon=(ImageView)row.findViewById(R.id.foodIcon);
            holder.tvFoodName=(TextView)row.findViewById(R.id.foodName);
            holder.cbSelectFood=(CheckBox)row.findViewById(R.id.selectFood);
            holder.cbSelectFood.setClickable(false);             
            holder.tvFoodDescription=(TextView)row.findViewById(R.id.foodDescription);
            holder.tvFoodQuantity=(TextView)row.findViewById(R.id.foodQuantity);
            holder.tvQuantity=(TextView)row.findViewById(R.id.quantity);
            row.setTag(holder);

        }       
        else
        {               
            holder=(TakeOrder)row.getTag();             
        }
        if(fl.bundleCategory.equals("Appetisers"))
        {
        holder.foodIcon.setImageResource(R.drawable.appetiser);
        }
        else
            if(fl.bundleCategory.equals("Main Courses"))
            {
                holder.foodIcon.setImageResource(R.drawable.main_course);
            }
            else
                if(fl.bundleCategory.equals("Sides"))
                {
                    holder.foodIcon.setImageResource(R.drawable.sides);
                }
                else
                    if(fl.bundleCategory.equals("Desserts"))
                    {
                        holder.foodIcon.setImageResource(R.drawable.desserts);
                    }
                    else
                        if(fl.bundleCategory.equals("Drinks"))
                        {
                            holder.foodIcon.setImageResource(R.drawable.drinks);
                        }
        holder.tvFoodName.setText(foodList.get(position).getMenuItem());
        holder.tvFoodDescription.setText(foodList.get(position).getDescription());
        holder.cbSelectFood.setChecked(checks.get(position));
        holder.tvQuantity.setText(String.valueOf(quantity.get(position)));      
        return row;     
    }

}

假设我选择(检查)两个列表项,然后按 addToOrder 按钮,它只会打印最后一次选择的项目两次。我不知道为什么它只是覆盖了我列表中的第一个对象我认为我的逻辑是正确的,但不知道为什么它不起作用?有人可以帮忙吗

4

1 回答 1

0

您应该在适配器中创建一个方法,让您更新当前的数组列表或添加/删除您传递的项目。但我看不出你是如何实现你的适配器的,这可能是最重要的代码......

你需要这样的东西,在你的适配器中(这是为了更新一个完整的列表):

public void updateListArray(ArrayList<Item> list) {
    this.clear();
    for(Item item : list) {
        this.add(item);
    }
    notifyDataSetChanged();
}
于 2012-05-19T11:25:44.973 回答