首页 > 解决方案 > 如何使用计时器/处理程序更新数组适配器?

问题描述

我从 API 获取数据,例如打印机名称、型号和状态,并显示在数组适配器的列表视图中。在我的程序中,我正在测试打印机是否空闲,并使用绿色(空闲)和红色(忙碌)圆圈显示在 listview 中,但我的问题是必须不断检查状态并更新以在屏幕上显示绿色或红色圆圈,对于这种情况,我正在检查处理程序和计时器并尝试实现它但没有成功 - 没有任何效果。我不确定哪个更好用以及如何使用,因为我使用的是数组适配器。

如果您有任何见解,请告诉我。

我的适配器类:

public class NsdServiceInfoAdapter extends ArrayAdapter<PrinterNew> {
private Context mContext;
private List<PrinterNew> services;
private InetAddress hostAddress;


//Creating new constructor with parameters such as this class(context), layout id (list item layout Id) and data model.

public NsdServiceInfoAdapter(@NonNull Context context, int layoutId, List<PrinterNew> list) {
    super(context, layoutId, list);
    mContext = context;
    services = list;
}

@NonNull
@Override


public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
    View listItem = convertView;

    //Checking if view is empty then we inflate our list  layout.
    if (listItem == null)
        listItem = 
LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);

    //Getting data's position in the data set.
    final PrinterNew currentService = services.get(position);



    final ImageView i = listItem.findViewById(R.id.status_circle);
    TextView t = listItem.findViewById(R.id.TextView_serviceName);
    final TextView r = listItem.findViewById(R.id.TextView_serviceIP);
    r.setText(currentService.getPrinterModel());
    t.setText(currentService.getPrinterName());


    if (currentService.isIdle()) {
        i.setColorFilter(Color.rgb(42, 187, 155));
    } else {
        i.setColorFilter(Color.rgb(240, 52, 52));
    }





    return listItem;
}

}

我的打印机课

public PrinterNew() {

}

public String getPrinterName() {
    return printerName;
}

public void setPrinterName(String printerName) {
    this.printerName = printerName;
}

public String getPrinterModel() {
    return printerModel;
}

public String getPrinterInformation() {
    return this.printInformation;
}

public void setPrinterInformation(String url, Context context, final VolleyCallback callback) {
    Log.d("API", "Currently calling URL " + url);
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("API", "Print information" + response);
            String temp = response.substring(2, response.length() - 2);
            byte msgArray[];
            try {
                msgArray = temp.getBytes("ISO-8859-1");
                state = msgArray[0];
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            printInformation = response;
            callback.onSuccess(printInformation);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("API", "Print information nope ");
            callback.onFailure(error);
        }
    }));
}

public String getMenuInformation() {
    return menuInformation;
}

public void setMenuInformation(String url, Context context, final VolleyCallback callback) {
    Log.d("API", "Currently calling URL " + url);

    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {


            String temp = response.substring(2, response.length() - 2);
            Log.d("API", "Current menu" + response);
            byte msgArray[];
            try {
                msgArray = temp.getBytes("ISO-8859-1");
                currentMenu = msgArray[0];
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }


            menuInformation = response;
            callback.onSuccess(menuInformation);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("API", "Current menu Nope ");
            callback.onFailure(error);
        }
    }));
}


public boolean isIdle() {
    if (state == -1 && currentMenu == 0) {
        return true;
    } else {
        return false;
    }
}


public void setPrinterService(NsdServiceInfo service) {
    this.printerService = service;
}

public NsdServiceInfo getPrinterService() {
    return this.printerService;
}


public interface VolleyCallback {
    void onSuccess(String result);

    void onFailure(Object response);
}

public void setPrinterModel(String url, Context context, final VolleyCallback callback) {
    Log.d("API", "Currently calling URL " + url);
    RequestQueue queue = Volley.newRequestQueue(context);
    queue.add(new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("API", "printer model nope ");
            callback.onFailure(error);
        }
    }));
}

}

标签: androidlistviewtimeradapterhandler

解决方案


使用回收站视图。

每当使用 recyclerview 列表中的数据发生变化时刷新您的列表,您需要用户adapter.notifyDataSetChanged()

但请注意,您需要在列表中获取确切的对象,更新它然后用上面的行刷新它。

希望这可以帮助。


推荐阅读