0

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

1.我想先加载15条消息。2.在列表的末尾应该有一个页脚 3.单击页脚或到达页脚后,它应该从不同的 url 加载下一个 15 条消息,而不是第一个 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(); 

                }
            });
        }



    }
}
4

1 回答 1

0

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

于 2012-05-17T22:34:14.497 回答