2016-06-16 6 views
3

Ich habe ein Fragment, in dem ich Listenansicht, die Daten vom Server enthält und auch eine Fußzeile am Ende der Listenansicht haben, die auch Serveranforderung senden, um weitere Elemente in Listview hinzuzufügen, Problem ist, dass, wenn Fußzeile kommt listview Scroll-Ende senden Serveranforderung back to back.pleas hilf mir, dieses Problem zu lösen.Wie Server-Anfrage gesendet, wenn Sie zum Ende der Listview scrollte?

Code für scroll Zuhörer

@Override 
public void onScrollStateChanged(AbsListView view, int scrollState) { 
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { 
     Log.i("a", "scrolling stopped..."); 
    } 
} 

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

    if (firstVisibleItem + visibleItemCount == totalItemCount&& totalItemCount != 0) { 
     if (!isloading) { 
      // It is time to add new data. We call the listener 
      isloading = true; 
      if (NetworkUtil.isConnected(getActivity())) { 
       m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data 
       m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above 

       sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string 
       sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string ///// 
       loadmoreData(); 
      } else { 
       Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show(); 
      } 

     } 
    } 
} 

Code für Load mehr Daten

public void loadmoreData() { 

    try { 
     String json; 
     // 3. build jsonObject 
     final JSONObject jsonObject = new JSONObject();// making object of Jsons. 
     jsonObject.put("agentCode", m_szMobileNumber);// put mobile number 
     jsonObject.put("pin", m_szEncryptedPassword);// put password 
     jsonObject.put("recordcount", sz_RecordCount);// put record count 
     jsonObject.put("lastcountvalue", sz_LastCount);// put last count 
     Log.d("CAppList:",sz_RecordCount); 
     Log.d("Capplist:",sz_LastCount); 
     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString();// convert Json object to string 

     System.out.println("Server Request:-" + json); 
     requestQueue = Volley.newRequestQueue(getActivity()); 

     jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 

       System.out.println("Response:-" + response); 
       try { 
        JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST 
        for (int i = 0; i < posts.length(); i++) { 
         JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I 
         item = new CDealAppDatastorage();// object create of DealAppdatastorage 
         item.setM_szHeaderText(post.getString("dealname"));//getting deal name 
         item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code 
         item.setM_szDealValue(post.getString("dealvalue")); 

         if (!s_oDataset.contains(item)) { 
          s_oDataset.add(item); 
         } 
        } 
        isloading=false; 
        m_oAdapter.notifyDataSetChanged(); 
        if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions 
         CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity()); 
        } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions ..... 
         CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity()); 
        } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) { 
         CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity()); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       System.out.println("Error:-" + error); 
       if (error instanceof TimeoutError) { 
        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection lost ! Please try again", getActivity()); 
       } else if (error instanceof NetworkError) { 
        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No internet connection", getActivity()); 
       } 
      } 
     }); 
     requestQueue.add(jsonObjectRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

Antwort

1

Sie können es in der folgenden Art und Weise tun:

private boolean bBottomOfView; 

@Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && bBottomOfView) { 
      if (NetworkUtil.isConnected(getActivity())) { 
       m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data 
       m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above 

       sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string 
       sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string ///// 
       loadmoreData(); 
      } else { 
       Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show(); 
      } 

     } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

     bBottomOfView = (firstVisibleItem + visibleItemCount) == totalItemCount ? true : false; 
} 
+0

bitte meinen Code bearbeiten – vishwas

+0

I Kumpel bearbeitet aber ich bin mir nicht sicher, was genau Sie versuchen, meine Standardanzahl zu erhöhen. –

+0

es funktioniert ... danke .... und eine weitere Sache kann ich footer in listview in onScroll Listener statt wo ich jetzt putted – vishwas

Verwandte Themen