2014-03-04 26 views
0

首先,我從服務器獲得數據,並且我編寫了ListView,現在我不想在列表視圖滾動到底部時加載更多內容。我的異步類有名稱所以我認爲我需要調用Fetch.execute();? 另外我需要使用我第一次調用的所有Fetch對象屬性值,我如何在onScroll ListView(Android)中啓動AsyncTask方法

感謝所有!
我的活動在這裏代碼

public class InboxActivity extends Activity implements View.OnClickListener { 


    ...................... 


    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { 

     ........ 

     public void onDateSet(DatePicker view, int year, int monthOfYear, 
       int dayOfMonth) { 
     ...........  

      this.fetch = new FetchTask(); 
      this.fetch.Selectedmonth = this.Smonth; 
      this.fetch.Selectedyear = this.Syear; 
      this.fetch.page = 0; 
      this.fetch.sess_id = user_id; 
      ListView ll = (ListView)findViewById(R.id.mailList); 
      this.fetch.ll = ll; 
      this.fetch.execute(); 
      ll.setOnScrollListener(new OnScrollListener(){ 


       public void onScrollStateChanged(ListView view, int scrollState) 
       { 



       } 

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




        if(firstVisibleItem+visibleItemCount==totalItemCount) 
        { 

        HERE I NEED TO LAUNCH FETCH object 

        }      

       } 



     } 
    }; 






public class FetchTask extends AsyncTask<Void, Void, JSONArray> { 

    HERE MY PRORPITIES 

     @Override 

     protected JSONArray doInBackground(Void... params) { 
      try { 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("my url"); 

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 


       nameValuePairs.add(new BasicNameValuePair("qw", "er")); 
       nameValuePairs.add(new BasicNameValuePair("debug", "1")); 
       nameValuePairs.add(new BasicNameValuePair("t", "0")); 
       nameValuePairs.add(new BasicNameValuePair("m", Integer.toString(this.Selectedmonth))); 
       nameValuePairs.add(new BasicNameValuePair("y", Integer.toString(this.Selectedyear))); 
       nameValuePairs.add(new BasicNameValuePair("st", Integer.toString(this.page))); 
       nameValuePairs.add(new BasicNameValuePair("sess_id", this.sess_id)); 


       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 


       HttpResponse response = httpclient.execute(httppost); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       sb.append(reader.readLine()); 
       String line = "0"; 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line); 
       } 
       reader.close(); 
       String result11 = sb.toString(); 
       System.out.println(result11); 
       this.result_str = result11; 
       // parsing data 
       JSONArray arr = new JSONArray(result11); 





       return new JSONArray(result11); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 



      @Override 
      protected void onPreExecute() { 
       this.pd = new ProgressDialog(InboxActivity.this); 
       this.pd.setMessage("loading"); 
       this.pd.show();   
      } 


     @Override 
     protected void onPostExecute(JSONArray result) 
     { 

      if (result != null) 
      { 
       List<String> subjects = new ArrayList<String>(); 
       List<String> emails = new ArrayList<String>(); 

       for(int i = 0; i < result.length(); i++) 
       { 
        try 
        { 
         JSONObject json_data = result.getJSONObject(i); 
         emails.add(json_data.getString("mittente")); 
         subjects.add(json_data.getString("oggetto")); 
        } 
        catch (JSONException e) 
        { 
         e.printStackTrace(); 
        } 

       } 
       if(this.page == 0) 
       { 
        this.adapter = new ArrayAdapter<String>(
          InboxActivity.this, 
          R.layout.da_item, 
          emails 
         ); 
        this.ll.setAdapter(this.adapter);    
       } 
       else 
       { 
        for(int i = 0; i < result.length(); i++) 
        { 
         JSONObject json_data; 
         try 
         { 
          json_data = result.getJSONObject(i); 
          this.adapter.add(json_data.getString("mittente")); 
         } 
         catch (JSONException e) 
         { 
          e.printStackTrace(); 
         } 

        } 
       } 
      } 
      else 
      { 
       System.out.println("Messages not found"); 

      }  
      this.pd.dismiss(); 
     } 
    } 
} 
+0

嘗試限制您發佈的代碼僅限於相關區域! –

+0

i'v deone它,請檢查它 – user3323180

回答

0

全面概述:

  1. 您將需要重新創建一個新獲取的對象。你不能重新執行一箇舊的AsynTask。

  2. 一個問題是你不知道有多少元素在你的列表視圖中,因爲滾動條會出現,就好像它到達最後。您的抓取可能會及時返回,並且添加更多元素,神奇地還有更多需要滾動的內容,或者它不能及時返回,並且用戶達到了明顯的目的......如果您可以提前確定總數,重寫GetCount方法。

  3. 決定何時取下一個數據集。如果您重寫適配器的getView方法,您將能夠在適配器爲每個位置創建視圖時獲得回調。隨着用戶滾動位置將更接近數組的末尾。設置一些閾值以開始獲取下一個數據集。

    類myAdapter擴展ArrayAdapter {

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        return super.getView(position, convertView, parent); 
    
        if(!thresholdReachedBefore && position > threshold){ 
    
         fetchNextDataSet(); 
    
        } 
    } 
    

    }

+0

但我需要重新創建新的獲取對象,當我需要加載下一個元素? – user3323180

+0

不僅可以重新創建它,而且必須更改用於查詢第一個數據集的所有參數,而不是檢索第二個數據集。重新創建很簡單:this.fetch = new FetchTask(); (done!) – NameSpace

+0

好的,我可以得到我的第一個抓取對象的屬性,並且比之後 this.fetch = new FetchTask() 傳遞它像這樣 this.fetch.page = FirstFetchObject.page ++; – user3323180

0

要運行的AsyncTask,您必須調用擴展的AsyncTask類的execute()方法。但要小心,因爲每次您執行提取操作時,您都將獲取一組可能導致重複的原始數據。解決這個問題的一種方法是僅返回一定數量的元素並對此進行計數。下次您執行抓取時,只需從下一個索引中獲取元素。