在Android上自动刷新屏幕

时间:2018-08-16 20:29:01

标签: android

我正在开发一个应用程序,并且在一个屏幕上显示从数据库中提取的订单。这些订单中通常包含一些物品。

我可以毫无问题地提取并显示数据,但是我正在努力实现一种自动刷新活动的方式,以便显示新订单(例如,每15秒刷新一次)。

我玩过一个处理程序,每15秒运行一次displayOrders()方法,但结果导致每15秒再次发布相同的订单。

这是我的代码:

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

}

适配器:

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



        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());
                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());
            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 = "http://mgourley03.web.eeecs.qub.ac.uk/RestaurantApp/clearOrder.php";
        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);
    }

有人能建议一种方法来使活动每15秒刷新一次,并且仅显示单个订单的列表(而不是每次刷新时都显示一些订单)

1 个答案:

答案 0 :(得分:0)

有很多方法可以处理这种情况,但首先,我将使用游标适配器列出您的数据。

  1. 然后您可以使用内容提供者和内容观察者,并在数据发生更改时进行通知
  2. 您可以使用会议室和实时数据
  3. 您可以使用SqlBrite并观察该表的更新,然后通知您的适配器重新加载