json async load msgs first 15然后在列表末尾加载15以后依此类推..直到结束

时间:2012-05-17 20:25:43

标签: android json asynchronous

我有异步列表视图,其中json被解析并显示在列表中。

1.我想首先加载15个msgs。 2.在列表的末尾应该有一个页脚 3.单击页脚或到达页脚后,它应该从不同的URL而不是第一个URL加载下一个15 msgs。

如何在同一列表中加载下一个15个msg,但这次是在异步中从不同的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(); 

                }
            });
        }



    }
}

1 个答案:

答案 0 :(得分:0)

只需下载新消息,将它们添加到适配器的myList列表中,然后在listView上运行notifyDataSetChanged()。请记住,更改适配器的数据集(向ArrayList添加项目,或只是通知适配器)应该在UI线程中进行。