今天要做的是總和前面練習的功能並組成一個小功能,我所集合出的功能是類點餐功能。
這邊用到的功能是AlertDialog的多選項勾選,將選取到的餐點傳送到下一個畫面放到RecyclerView中顯示並調整數量。
AlertDialog.Builder builder1 = new AlertDialog.Builder (this);
builder1.setTitle ("食物清單");
builder1.setMultiChoiceItems (Foods, selected, new DialogInterface.OnMultiChoiceClickListener () {
@Override
public void onClick (DialogInterface dialogInterface, int i, boolean b) {
//判別Boolean函數是否有被選取,以及取消後的清單刪除方法。
if (b){
Toast.makeText (MainActivity.this,"Now you selected Food is : "+Foods[i],Toast.LENGTH_SHORT).show ();
foods[i] = Foods[i];
} else {
Toast.makeText (MainActivity.this,"Now you cancel Food :"+Foods[i],Toast.LENGTH_SHORT).show ();
foods[i] = null;
}
}
}).setPositiveButton ("送出", new DialogInterface.OnClickListener () {
@Override
public void onClick (DialogInterface dialogInterface, int i) {
// 存放過濾null後的結果清單
List<String> result = new ArrayList<>();
// 資料處理, 過濾null
for (int k=0; k<Foods.length; k++){
if (foods[k] == null) {
Log.e ("NULL", "onClick: "+"此為空值" );
} else {
Log.e ("TAG", "onClick: " + foods[k]);
result.add (foods[k]);
}
}
// 跳頁, 並傳食物清單
Intent intent = new Intent (MainActivity.this, QuantitySelectActivity.class);
bundle.putStringArray (QuantitySelectActivity.PARAM_FOOD, result.toArray(new String[0]));
intent.putExtras (bundle);
startActivity (intent);
Toast.makeText (MainActivity.this,"已送出",Toast.LENGTH_SHORT).show ();
}
}).setNegativeButton ("取消選取", new DialogInterface.OnClickListener () {
@Override
public void onClick (DialogInterface dialogInterface, int i) {
Log.e ("TAG", "已取消選取");
Toast.makeText (MainActivity.this, "取消選取", Toast.LENGTH_SHORT).show ();
}
}).setIcon (R.drawable.foods);
builder1.create ().show ();
MultiSelect.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
MultiSelectAlertDialog ();
}
});
private final String[] dataList;
//透過這個實踐一個接口,傳入的資料是String陣列的資料。
public QuantityListAdapter(String[] dataList) {
this.dataList = dataList;
}
//創建以及綁定元件
static class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameSetting,Number;
public Spinner quantityspinner;
ArrayAdapter<CharSequence> adapter;
public ViewHolder(@NonNull View itemView) {
super(itemView);
NameSetting = itemView.findViewById(R.id.name);
quantityspinner = itemView.findViewById (R.id.quantityspinner);
}
public void itemView(HashMap<String, String> stringStringHashMap) {
}
}
//建立Spinner以及TextView強制寫入List的資料。
@Override
public void onBindViewHolder(@NonNull QuantityListAdapter.ViewHolder holder, int position) {
holder.NameSetting.setText (dataList[position]);
// Spinner的Adapter
holder.adapter = ArrayAdapter.createFromResource (
holder.itemView.getContext (),
R.array.quantity,
android.R.layout.simple_spinner_dropdown_item);
holder.quantityspinner.setAdapter (holder.adapter);
}
//接收前面傳回的陣列大小&陣列內容
Bundle bundle = getIntent ().getExtras ();
// 取得到的食物清單
getFoods = bundle.getStringArray (PARAM_FOOD);
// 建立RecyclerView(呼叫Adapter)。
binding.recyclerView.setLayoutManager (new LinearLayoutManager (this));
binding.recyclerView.addItemDecoration (new DividerItemDecoration (this,DividerItemDecoration.VERTICAL));
quantityListAdapter = new QuantityListAdapter (getFoods);
binding.recyclerView.setAdapter (quantityListAdapter);