使用适配器时在Android上刷新活动

时间:2018-08-20 20:37:32

标签: android android-adapter

我有一个Android应用程序,厨师可以在其中查看客户订单列表。每个订单都有自己的按钮,单击该按钮可以更新该订单在数据库中的可见性。这段代码行之有效。

但是,单击按钮后,我希望主厨的视图自动更新以使订单不再显示在屏幕上。目前,他们必须返回并重新加载屏幕才能消失。有没有人有什么建议?我已经考虑过重新加载活动,但是由于清除按钮的功能在适配器类中,而不是在厨师活动中,所以这是行不通的。

这是厨师班:

public class ChefScreen extends AppCompatActivity {

    ListView listView;
    List<ChefOrderList> listOrders;

    Button requestWaiter;


    @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<>();

        requestWaiter = findViewById(R.id.btnChefToWaiter);
        requestWaiter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWaiter();
            }
        });

        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"), orderObj.getInt("table_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);
    }


}

Model类:

public class ChefOrderList {

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

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


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

    public int getTable() {
        return Table;
    }
}

最后是适配器! completeOrder方法是单击清除订单按钮后执行的方法。

public class ChefOrderAdapter extends ArrayAdapter<ChefOrderList> {

    private List<ChefOrderList> chefOrderList1;
    private Context context;
    private Button completeOrder;




    public ChefOrderAdapter(List<ChefOrderList> M, Context C){
        super(C, R.layout.listcheforders, M);
        this.chefOrderList1 = M;
        this.context = C;

    }

    @Override
    public View getView(final 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);
        TextView table = view.findViewById(R.id.tvTable);



        completeOrder = view.findViewById(R.id.btnComplete);

        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());
                table.setText("Table: " +chefOrderList.getTable());
                completeOrder.setVisibility(View.VISIBLE);
                completeOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        completeOrder(position);
                    }
                });

            }

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

        } else {

            itemType.setText(chefOrderList.getType());
            orderNumber.setText("Order: " +chefOrderList.getOrder());
            orderTime.setText(chefOrderList.getDate());
            table.setText("Table: " +chefOrderList.getTable());
            completeOrder.setVisibility(View.VISIBLE);
            completeOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    completeOrder(position);
                }
            });

        }
        return view;

    }

    private void completeOrder(final int position){
        String url = "hidden";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if(response.trim().equals("success")){
                    Toast.makeText(context, "Order completed!", Toast.LENGTH_LONG).show();

                    } else {
                        Toast.makeText(context, "Clear order failed!", Toast.LENGTH_LONG).show();
                    }
                }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, "Error!" +error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {


                Map<String, String> params = new HashMap<>();
                params.put("orderid", String.valueOf(chefOrderList1.get(position).getOrder()));
                return params;
            }
        };
        MySingleton.getInstance(context).addToRequestQueue(stringRequest);

    }
}

1 个答案:

答案 0 :(得分:1)

据此行了解

  

如果厨师的视图自动更新,以便   订单不再显示在他们的屏幕上。

基于客户操作的数据库更新;例如关闭订单或取消订单。对吧?

我认为您应该使用Socket或Push Notification。

随行推送通知“ FCM”

当客户执行影响数据库中订单的操作时,您应使用其令牌向厨师发送通知,并在应用程序中处理该通知以撤回

displayOrders方法

并使用recyclerview.notifiDataSetChange();

否则,您应该重新连接到数据库以检查是否发生任何更改。 “不推荐”