我在另一个文件中有一个带有listview和ArrayAdapter类的MainActivity Class文件。
在ArrayAdapter类中,我在listitem中有一个按钮,使用Volley来更新远程Rest服务器中的记录。
我已经阅读了关于Volley的取消请求:
@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
mRequestQueue.cancelAll(TAG);
}
}
我的疑问是:在某些Android生命周期事件中我是否需要注意取消排球请求? 如果是,我该如何进入arrayadapter类?
public class MainActivity extends Activity {
ArrayList<ObjectQueue> lista = new ArrayList<ObjectQueue>();
ListviewQueueAdapter lvA;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle args = new Bundle();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listmain);
View v = (View) findViewById(R.id.emptymain);
lv.setEmptyView(v);
lvA = new ListviewQueueAdapter(MainActivity.this, lista);
lv.setAdapter(lvA);
lvA.setData(lista);
}
我的arrayadapter类是:
public class ListviewQueueAdapter extends ArrayAdapter<ObjectQueue> implements Filterable {
ArrayList<ObjectQueue> items; //working values
ArrayList<ObjectQueue> origitems; // data work
private Activity context;
RequestQueue reqQueue; //Volley request
public ListviewQueueAdapter(Activity context, ArrayList<ObjectQueue> items) {
super(context, R.layout.singlerowqueue, items);
this.context = context;
this.items=items;
}
public void add(ObjectQueue oq){
this.items.add(oq);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ObjectQueue o = items.get(position);
final ViewHolder holder;
View vi = convertView;
if (vi == null){
LayoutInflater inflater = context.getLayoutInflater();
vi = inflater.inflate(R.layout.singlerowqueue, null);// e' preciso fazer o inflate pois a view nao ainda nao esta em activity alguma
holder = new ViewHolder();
holder.mbut_stat = (Button) vi.findViewById(R.id.but_status);
....
vi.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mbut_stat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setTitle("Mudar STATUS");
String[] types = {"Normal", "Urgente", "Pendente", "Atendido"};
b.setItems(types, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
update_fila(o, which + 1);
}
});
b.show();
}
});
......
......
return vi;
}
static class ViewHolder {
Button mbut_stat;
}
private void update_fila(final ObjectQueue ob, final int st) {
reqQueue = Volley.newRequestQueue(context);
String url = Common.getServerURL() + "\"UpdateStatusFila\"";
// constructs the POST body using the parameters
JSONArray ja = new JSONArray();
...
FillSomeArray(ja);
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("_parameters", ja);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url,
jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
try {
dosomething()(;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
jr.setRetryPolicy(new DefaultRetryPolicy(Common.DEFAULT_TIMEOUT_MS,
Common.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
reqQueue.add(jr);
}
答案 0 :(得分:0)
您不需要在生命周期事件中取消请求。如果请求不再相关或有用,那么为用户节省带宽和电池寿命将是一个很好的礼仪。
你把它放在哪里是一个取决于你的特定需求的设计决定,但是你可以为你的适配器添加一个方法:
public void onStop () {
mRequestQueue.cancelAll(TAG);
}
并从活动传播生命周期调用:
@Override
protected void onStop () {
super.onStop();
adapter.onStop();
}