チェックボックス付きのカスタムリストビューを使用する必要がある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;
}
}
2 つのリスト項目を選択 (チェック) し、addToOrder ボタンを押すと、最後に選択した項目が 2 回印刷されます。リストの最初のオブジェクトをオーバーライドする理由がわかりません。私のロジックは正しいと思いますが、なぜ機能しないのかわかりませんか?? 誰でも助けてください