首页 > 解决方案 > Android - 将边框动态应用于一组 TextView

问题描述

我想在从数据库中检索的屏幕上显示订单。每个订单上都有各种物品,屏幕会显示各种订单及其物品的详细信息。

例如:

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

订单 2 项目 4 项目 5

,ETC。

我能够毫无问题地返回这些数据,但是我想知道如何在每个订单周围设置边框(而不是每个易于执行的项目)。

这就是现在返回的样子(抱歉,它没有在这张图片中显示我想要的一切)。基本上我想要每个特定订单周围的边框,所以我想要边框中的订单 1 中的所有项目,然后我想要边框中的订单 2 中的所有项目。

在此处输入图像描述

代码概述如下,任何帮助将不胜感激:

这是显示订单的活动:

public class ChefScreen extends AppCompatActivity {

    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);
    }
}

这是厨师屏幕的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChefScreen">

    <ListView
        android:id="@+id/list_chef_orders"
        android:layout_width="330dp"
        android:layout_height="430dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="81dp"
        tools:layout_editor_absoluteX="20dp"
        tools:layout_editor_absoluteY="77dp" />

</RelativeLayout>

适配器:

public class ChefOrderAdapter extends ArrayAdapter<ChefOrderList> {

    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;

    }

}

模型类:

    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;
    }
}

最后是列表或订单的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">



    <TextView
        android:id="@+id/tvOrderNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/tvItemName"
        android:layout_marginTop="59dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#000"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvDateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/tvItemName"
        android:layout_marginTop="15dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#000" />

    <TextView
        android:id="@+id/tvItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="166dp"
        android:text="placeholderName"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#000" />

    <TextView
        android:id="@+id/tvItemQty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/tvItemName"
        android:layout_marginTop="216dp"
        android:text="qty"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#000" />

    <TextView
        android:id="@+id/tvItemType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tvItemName"
        android:layout_alignStart="@+id/tvItemName"
        android:layout_marginBottom="-166dp"
        android:paddingBottom="20dp"
        android:text=""
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textColor="#000"
        android:textSize="26sp"
        android:textStyle="italic"
        tools:text="text" />



</RelativeLayout>

标签: androidlistviewtextview

解决方案


推荐阅读