在列表视图中创建列表视图

时间:2018-08-15 18:24:20

标签: android listview

我正在创建一个显示来自两个数据库表的订单的android应用程序。

一个表显示order_id,另一表显示每个订单的商品。

我要显示所有订单的列表视图,并且在每个订单中,我要显示所有订单项目,如下所示:

订单1 项目1 项目2 项目3

订单2 项目4 项目5,等等

到目前为止,我只能将其作为一个列表视图返回,该列表视图返回如下:

Order1 项目1

Order1 Item2

Order1 Item3,等等

任何建议都是有帮助的,我真的为此感到困惑!

我的代码如下:

我希望在其中显示的活动:

ListView listView;
    List<ChefOrderList> listOrders;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chef_screen);

        listView = findViewById(R.id.list_chef_orders);
        listOrders = new ArrayList<>();

        displayOrders();
    }

    private void displayOrders(){
        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("orders");

                    for (int i = 0; i < array.length(); i++) {
                        final JSONObject orderObj = array.getJSONObject(i);

                        ChefOrderList c = new ChefOrderList(orderObj.getString("menu_item_name"), orderObj.getString("item_type"), orderObj.getString("order_date_time"),
                                orderObj.getInt("quantity_ordered"), orderObj.getInt("order_id"));
                        listOrders.add(c);


                    }
                    ChefOrderAdapter adapter = new ChefOrderAdapter(listOrders, getApplicationContext());
                    listView.setAdapter(adapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(ChefScreen.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", "0");
                return params;
            }

        };

        MySingleton.getInstance(this).addToRequestQueue(stringRequest);
    }

模型:

public class ChefOrderList {

   String Name, Type, Date;
    int Qty, Order;

    public ChefOrderList(String name, String type, String date, int qty, int order) {
        Name = name;
        Type = type;
        Date = date;
        Qty = qty;
        Order = order;
    }


    public String getDate() {
        return Date;
    }

    public String getName() {
        return Name;
    }

    public String getType() {
        return Type;
    }

    public int getQty() {
        return Qty;
    }

    public int getOrder() {
        return Order;
    }
}

适配器:

private List<ChefOrderList> chefOrderList1;
    private Context context;

    public ChefOrderAdapter(List<ChefOrderList> M, Context C){
        super(C, R.layout.listcheforders, M);
        this.chefOrderList1 = 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.listcheforders,null,true);

        TextView orderNumber = view.findViewById(R.id.tvOrderNumber);
        TextView itemType = view.findViewById(R.id.tvItemType);
        TextView itemName = view.findViewById(R.id.tvItemName);
        TextView orderQuantity = view.findViewById(R.id.tvItemQty);
        TextView orderTime = view.findViewById(R.id.tvDateTime);


        ChefOrderList chefOrderList = chefOrderList1.get(position);


        itemName.setText(chefOrderList.getName());
        orderQuantity.setText("Qty: " +chefOrderList.getQty());


        if(position>0){
            ChefOrderList prevChefOrderList = chefOrderList1.get(position-1);
            if(chefOrderList.getOrder() != (prevChefOrderList.getOrder())){
                orderNumber.setText("Order: " +chefOrderList.getOrder());

                orderTime.setText(chefOrderList.getDate());

            }

            if(!chefOrderList.getType().equals(prevChefOrderList.getType())){
                itemType.setText(chefOrderList.getType());
            }

        } else {

            itemType.setText(chefOrderList.getType());
            orderNumber.setText("Order: " +chefOrderList.getOrder());
            orderTime.setText(chefOrderList.getDate());
        }
        return view;

    }

查询和表: enter image description here

1 个答案:

答案 0 :(得分:0)

关于“订购”模型和“项目”模型怎么样。然后,您的Order模型可以具有Item对象的列表。当您在列表适配器中扩展视图时,可以遍历订单对象上的项目并创建一个新视图并将其添加到要显示的视图中