我正在为我正在处理的项目的一部分创建订单应用程序。基本上,用户可以在一个活动中以ListView的形式查看从数据库中提取的所有项目的列表。他们还具有一个EditText字段,可在其中输入每个项目下方要订购的数量。
我想在屏幕底部有一个提交按钮,然后将发布所有已订购商品的详细信息(即商品ID)和订购数量,但是我不确定该怎么做。
这是我当前的代码:
public class OrderItems extends AppCompatActivity {
String menuID;
private TextView menuTitle;
ListView listView;
List<OrderItemList> listOrderItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_items);
menuTitle = findViewById(R.id.tvOrderMenuTitle);
Intent intent = getIntent();
String title = intent.getStringExtra("title");
menuID = intent.getStringExtra("id");
menuTitle.setText(title);
listView = findViewById(R.id.list_order_items);
listOrderItems = new ArrayList<>();
displayItems();
}
private void displayItems(){
String url = "hidden";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("menuitems");
for (int i = 0; i < array.length(); i++) {
final JSONObject menuObj = array.getJSONObject(i);
OrderItemList o = new OrderItemList(menuObj.getString("menu_item_name"),
menuObj.getString("item_type"), menuObj.getInt("menu_item_price"), menuObj.getInt("menu_item_qty"));
listOrderItems.add(o);
}
OrderItemAdapter adapter = new OrderItemAdapter(listOrderItems, getApplicationContext());
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(OrderItems.this, "Oops! " +error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("menuid", menuID);
return params;
}
};
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
}
尽管我无法在EditText框中键入要订购的项目数量,但显示项目的效果很好,一旦键入然后转到下一个框,先前的值就会消失。
模型类如下:
public class OrderItemList {
String Name, Type;
int Price, Qty;
public OrderItemList(String name, String type, int price, int qty) {
Name = name;
Type = type;
Price = price;
Qty = qty;
}
public String getName() {
return Name;
}
public int getQty() {
return Qty;
}
public String getType() {
return Type;
}
public int getPrice() {
return Price;
}
}
和适配器:
public class OrderItemAdapter extends ArrayAdapter <OrderItemList>{
private List<OrderItemList> orderItemList1;
private Context context;
public OrderItemAdapter(List<OrderItemList> M, Context C){
super(C, R.layout.listorder, M);
this.orderItemList1 = M;
this.context = C;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.listorder,null,true);
TextView menuItemName = view.findViewById(R.id.tvItemName);
TextView menuItemPrice = view.findViewById(R.id.tvItemPrice);
TextView menuItemType = view.findViewById(R.id.tvItemType);
TextView menuItemQty = view.findViewById(R.id.tvItemQty);
OrderItemList orderItemList = orderItemList1.get(position);
menuItemName.setText(orderItemList.getName());
menuItemQty.setText("Available quantity: " +orderItemList.getQty());
menuItemPrice.setText("£" +orderItemList.getPrice());
if(position>0){
OrderItemList prevOrderItemList = orderItemList1.get(position-1);
if(!orderItemList.getType().equals(prevOrderItemList.getType())){
menuItemType.setText(orderItemList.getType());
}
} else {
menuItemType.setText(orderItemList.getType());
}
return view;
}
}
任何帮助/部门将不胜感激!