Android-将边框动态应用于TextViews组

时间:2018-08-15 14:21:53

标签: android listview textview

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

例如:

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

Order2 项目4 项目5

我能够毫无问题地返回此数据,但是我想知道如何在每个订单(而不是容易做到的每个项目)周围加上边框。

这就是现在的返回结果(很抱歉,它没有显示此图像中我想要的所有内容)。基本上,我希望每个特定订单周围都有边框,所以我想要的订单1中的所有项目都在边框中,然后我想要在订单2中的所有项目都在边框中。

enter image description here

下面概述了代码,我们将不胜感激:

这是显示订单的活动:

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>

0 个答案:

没有答案