gpt4 book ai didi

android - 在 ListView 中创建 ListView

转载 作者:行者123 更新时间:2023-11-29 02:26:42 25 4
gpt4 key购买 nike

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

一个表显示 order_id,另一个表显示每个订单的项目。

我想显示所有订单的 ListView ,并且在每个订单中我想显示所有订单项目,如下所示:

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

订单 2第 4 项第 5 项等

到目前为止,我只能将其作为一个 ListView 返回,返回如下:

订单1项目1

订单1项目2

订单1Item3等

任何建议都会有所帮助,我真的很挣扎!

我的代码如下:

我想要显示它的 Activity :

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

最佳答案

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

关于android - 在 ListView 中创建 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51864347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com