0
我正在開發一個android應用程序,我必須使用自定義listview和複選框。當用戶選中/取消選中時,我需要分別添加/刪除數組列表中的對象。ArrayList添加和刪除對象
這裏是我的代碼:
@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按鈕,它只打印最後一次選擇的項目兩次。我不知道爲什麼它只是重寫我的名單中的第一個對象 我認爲我的邏輯是正確的,但不知道爲什麼它不工作? 任何人都可以請幫助
如果你看到我的onItemClick方法在else body中檢查是否複選框沒有被選中,,,所以即時檢查它,並在我的警報對話框中的數組列表中添加對象確定按鈕功能 –
是的我看到你添加元素如果我是正確的,那麼訂單。但是orderlist只是一個新的列表......你永遠不會將列表的元素添加到你的適配器中。 – Warpzit
我想添加或刪除我的列表中的檢查/取消選中條件,因爲我有一個apllication需要這種類型的實現 –