凌空不会停止添加日期从JSON来的ListView [英] Volley does not stop adding date to listview from json

查看:118
本文介绍了凌空不会停止添加日期从JSON来的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想应用程序,以显示日期列表视图。我的应用程序有 TabLayout 模仿的这个列表视图这模仿的这个的。所以,标签有列表视图。显示在列表视图所有JSON日期后,他们装载JSON再次日期。
它发生时,我刷每个标签如 TAB1→TAB2→TAB3→TAB1→TAB3 ...
这是可能code。如果有人知道什么是错的,请教我。照片

 公共类AppController的扩展应用{
公共静态最终字符串变量= AppController.class.getSimpleName();
私人请求队列mRequestQueue;
私人ImageLoader的mImageLoader;
私有静态AppController的mInstance;

@覆盖
公共无效的onCreate(){
    super.onCreate();
    mInstance =这一点;
}
公共静态同步的AppController的getInstance(){
    返回mInstance;
}
公共请求队列getRequestQueue(){
    如果(mRequestQueue == NULL){
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    返回mRequestQueue;
}
公共ImageLoader的getImageLoader(){
    getRequestQueue();
    如果(mImageLoader == NULL){
        mImageLoader =新ImageLoader的(this.mRequestQueue,
                新LruBitmapCache());
    }
    返回this.mImageLoader;
}

公众< T>无效addToRequestQueue(请求< T> REQ,字符串变量){
    //设置默认如果tag是空的
    req.setTag(?TextUtils.isEmpty(标签)标签:标签);
    。getRequestQueue()加(REQ);
}
公众< T>无效addToRequestQueue(请求< T> REQ){
    req.setTag(TAG);
    。getRequestQueue()加(REQ);
}
公共无效cancelPendingRequests(对象标签){
    如果(mRequestQueue!= NULL){
        mRequestQueue.cancelAll(标签);
    }
}
}
 


选项卡1(TAB2和TAB3几乎是一样的)

 公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    适配器=新CSadapter(getActivity(),movieList);
    setListAdapter(适配器);
    getListView()setScrollingCacheEnabled(假)。
    pDialog =新ProgressDialog(getActivity());
    //显示进度对话框发出HTTP请求之前
    pDialog.setMessage(载入中...);
    pDialog.show();
    //创建凌空要求OBJ
    JsonArrayRequest movieReq =新JsonArrayRequest(URL,
            新Response.Listener< JSONArray>(){
                @覆盖
                公共无效onResponse(JSONArray响应){
                    Log.d(TAG,response.toString());
                    hidePDialog();
                    //解析JSON
                    的for(int i = 0; I< response.length();我++){
                        尝试 {
                            JSONObject的OBJ = response.getJSONObject(我);
                            宋电影=新曲();
                            movie.setTitle(obj.getString(标题));
                            movie.setThumbnailUrl(obj.getString(外套));
                            movie.setNumber(obj.getString(数字));
                            movie.setYear(obj.getString(释放));
                            //添加电影电影阵列
                            movieList.add(电影);
                        }赶上(JSONException E){
                            e.printStackTrace();
                        }
                    }
                    //通知有关数据变化表适配器
                    //使之呈现列表视图更新数据
                    adapter.notifyDataSetChanged();
                }
            },新Response.ErrorListener(){
        @覆盖
        公共无效onErrorResponse(VolleyError错误){
            VolleyLog.d(TAG,错误:+ error.getMessage());
            hidePDialog();
        }
    });
    //添加请求,请求队列
    。AppController.getInstance()addToRequestQueue(movieReq);
}
 

解决方案

在寻呼机的默认页限制为1。如果你超越你的网页活动创建again.This可能是这种情况发生here.You可以增加页数的限制,以避免重新创建

I want app to show dates in listview. My app has TabLayout which imitates this and Listviews which imitate this.So, tabs have listview. After showing all json dates in listview, they load json dates again.
It happens when I swipe each tabs like tab1→tab2→tab3→tab1→tab3....
This is may code. If anyone knows what is wrong, please teach me.

public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}
public static synchronized AppController getInstance() {
    return mInstance;
}
public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}
public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}


Tab 1 (Tab2 and Tab3 are almost same)

    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    adapter = new CSadapter(getActivity(),movieList);
    setListAdapter(adapter);
    getListView().setScrollingCacheEnabled(false);
    pDialog = new ProgressDialog(getActivity());
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();
    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            Song movie = new Song();
                            movie.setTitle(obj.getString("title"));
                            movie.setThumbnailUrl(obj.getString("jacket"));
                            movie.setNumber(obj.getString("number"));
                            movie.setYear(obj.getString("release"));
                            // adding movie to movies array
                            movieList.add(movie);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}

解决方案

In a pager the default page limit is 1 .If you go beyond that your page activity is created again.This may be the case happening here.You can increase the page limit to avoid re creation

这篇关于凌空不会停止添加日期从JSON来的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆