Android 如果截击结束了,如何获得?
这是我的代码,我想从db中填充列表,然后使用它,但我遇到了问题,它正在执行Asychrouz,当我试图从这个列表中获取值时,我遇到了越界异常,我应该如何重新设置这个方法的样式,或者我应该如何从Mainactivity调用来解决这个问题Android 如果截击结束了,如何获得?,android,android-volley,Android,Android Volley,这是我的代码,我想从db中填充列表,然后使用它,但我遇到了问题,它正在执行Asychrouz,当我试图从这个列表中获取值时,我遇到了越界异常,我应该如何重新设置这个方法的样式,或者我应该如何从Mainactivity调用来解决这个问题 public class VolleySingleton extends Application { public static final String TAG = VolleySingleton.class.getSimpleName(); private R
public class VolleySingleton extends Application {
public static final String TAG = VolleySingleton.class.getSimpleName();
private RequestQueue requestQueue;
private static VolleySingleton instance;
public static int count=0;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static synchronized VolleySingleton getInstance() {
return instance;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
count++;
}
public <T> void addToRequestQueue(Request<T> request) {
getRequestQueue().add(request);
}
public void cancelPendingRequests(Object tag) {
if (requestQueue != null) {
requestQueue.cancelAll(tag);
}
}
}
您可以在中处理数据 公共无效OnResponseJ声纳射线响应{ //过程响应 } 作为回应,Volley完成了工作。
您可以在此处设置回调以在您的类中发送响应。我如何才能做到这一点?1。在ResponseReceived{public void responseReceivedJSONArray response;}上创建接口公共接口2.在活动3中实现此接口。在调用活动4的截取方法getRoomArray传递实例时。现在,您可以在截取响应中调用方法responseReceivedjsonArray并将数组传递给该方法。因此,您将在活动中获得该数组。我没有否决,实际上,在这个接口的实现中要做什么并不十分清楚:/在您的活动中实现这个接口,并在活动中重写它的方法公共类A扩展活动实现OnResponseReceived{public void responseReceivedJSONArray jsonArray{//parse json here and add to DB}好的,我已经这样做了,但是得到了相同的错误,它仍然是异步的,当我试图获取list的一个元素getting of BoundException时
private static String TAG = JSonParse.class.getSimpleName();
public static List<Room> getRoomArray(String url, Context context) throws InterruptedException {
final List<Room> roomList = new ArrayList<>();
final CountDownLatch countDownLatch = new CountDownLatch(1);
JsonArrayRequest arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
Room room = null;
for (int i = 0; i < response.length(); i++) {
room = new Room();
JSONObject newRoom = (JSONObject) response.get(i);
room.setId(newRoom.getLong("id"));
room.setName(newRoom.getString("name"));
room.setActive(newRoom.getBoolean("active"));
roomList.add(room);
}
Helper.initList(roomList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
}
});
VolleySingleton.getInstance().addToRequestQueue(arrayRequest);
return roomList;
}