2012-05-17 24 views
0

我有異步listview,json在列表中被解析和顯示。json async load msgs first 15然後在列表的最後加載15更多等等..直到結尾

1.我想先加載15個郵件。 2.在列表的末尾應該有一個頁腳 3.點擊頁腳或到達頁腳後,它應該從不同的url加載下一個15 msgs,而不是第一個url。

如何在同一個列表中加載下一個15個信息 - 但是這次是以異步方式從不同的URL加載。

鏈接我已經提到至今:http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/

附加代碼:

public class btn extends ListActivity { 
    /** Called when the activity is first created. */ 
    SimpleAdapter adapter; 
    JSONArray earthquakes; 
    String jString; 
    ArrayList<HashMap<String, String>> mylist; 
    String magni; 
    ProgressDialog pd; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 
     mylist = new ArrayList<HashMap<String, String>>(); 


     new loadsomestuff().execute(); 




    } 

    public class loadsomestuff extends AsyncTask<String, Integer, String>{ 


     protected void onPreExecute() { 
      pd=new ProgressDialog(btn.this); 
      pd.setTitle("Loading......Please wait"); 
      pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      pd.show(); 


     } 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 

      jString="{\"earthquakes\": [{ \"eqid\": \"c0001xgp\", \"magnitude\": 8.8, \"lng\": 142.369, \"src\": \"us\", \"datetime\": \"2011-03-11 04:46:23\", \"depth\": 24.4,\"lat\": 38.322 },{ \"eqid\": \"2007hear\",\"magnitude\": 8.4,\"lng\": 101.3815,\"src\": \"us\", \"datetime\": \"2007-09-12 09:10:26\",\"depth\": 30,\"lat\": -4.5172},{ \"eqid\": \"2007hear\",\"magnitude\": 8.5,\"lng\": 101.3815,\"src\": \"us\", \"datetime\": \"2007-09-12 09:10:26\",\"depth\": 30,\"lat\": -4.5172},{ \"eqid\": \"2007hear\",\"magnitude\": 8.6,\"lng\": 101.3815,\"src\": \"us\", \"datetime\": \"2007-09-12 09:10:26\",\"depth\": 30,\"lat\": -4.5172},{ \"eqid\": \"2007hear\",\"magnitude\": 8.7,\"lng\": 101.3815,\"src\": \"us\", \"datetime\": \"2007-09-12 09:10:26\",\"depth\": 30,\"lat\": -4.5172}]}"; 
      // JSONObject json=JSONfunctions.getJSONfromURL(jString); 
      JSONObject jObject = null; 
      try { 
       jObject = new JSONObject(jString); 
      } catch (JSONException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try{ 

       earthquakes = jObject.getJSONArray("earthquakes"); 

       for(int i=0;i<earthquakes.length();i++){       
        HashMap<String, String> map = new HashMap<String, String>();  
        JSONObject e = earthquakes.getJSONObject(i); 

        magni= e.getString("magnitude"); 


        map.put("id", String.valueOf(i)); 
        map.put("name", "Earthquake name:" + e.getString("eqid")); 
        map.put("magnitude", "Magnitude: " + e.getString("magnitude")); 
        mylist.add(map); 

        publishProgress(((int) ((i/(float) earthquakes.length()) * 100))); 
       } 
       adapter = new SimpleAdapter(getBaseContext(), mylist , R.layout.main, 
         new String[] { "name", "magnitude" }, 
         new int[] { R.id.item_title, R.id.item_subtitle }); 

      }catch(JSONException e)  { 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

      return null; 
     } 
     protected void onProgressUpdate(Integer...integers) { 

      pd.incrementProgressBy(integers[0]); 

     } 

     @Override 
     protected void onPostExecute(String result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 


      setListAdapter(adapter); 

      pd.dismiss(); 

      final ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        @SuppressWarnings("unchecked") 
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
        Toast.makeText(btn.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

       } 
      }); 
     } 



    } 
} 
+0

你的名單有多大? –

+0

它會先是15,然後當他到達列表末尾時再加15,這應該繼續,直到他所有的消息都從服務器上顯示出來。所以它是一個很長的列表。但是這是流程。 –

回答

0

只需下載新郵件,將它們添加到您的適配器myList中列表,並在你的ListView運行notifyDataSetChanged()。請記住,應該在UI線程中更改適配器的數據集(向ArrayList添加項目或僅通知適配器)。

+0

感謝您的回覆,我已經使用了Onscroll項目點擊,它給了我一個在列表結尾的敬酒。如何顯示加載欄直到它加載?我應該使用runnable函數進度對話框還是Async可以爲我處理? –

+0

你想在最後一個項目上或在用戶的前面加載欄? – Seraphis

+0

我設法使它工作通過在用戶面前加載欄,但在滾動活動觸發多次,我如何設法觸發它只有一次?再次感謝。 –

相關問題