无尽的ListView

时间:2012-02-06 21:02:54

标签: android android-listview

我有一个列表视图,从远程mysql数据库中检索数据并以json格式返回结果。该查询包括日期字段。我想实现一个无限列表函数,以便它一次加载20个结果,并且可能是date_field中每天的标题。我想首先关注逐步加载列表。

我是否运行整个查询,只是以我想要的增量返回列表视图中的结果,或者我是否必须修改查询?

以下是我检索数据的代码:

private class LoadList extends AsyncTask<String, UserRecord, JSONArray> {
    protected JSONArray doInBackground(String... link) {

        URI uri = null;
        try {
            uri = new URI(link[0]);
        } catch (URISyntaxException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(uri);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");

            String line="0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        try{
            jArray = new JSONArray(result);
            JSONObject json_data=null;

            displayname = new String[jArray.length()];
            song_name = new String[jArray.length()];
            artist = new String[jArray.length()];
            description = new String[jArray.length()];
            genre = new String[jArray.length()];
            custom_genre = new String[jArray.length()];
            album = new String[jArray.length()];
            timestamp = new String[jArray.length()];
            song_id  = new String[jArray.length()];
            avatar = new String[jArray.length()];
            drawable = new Drawable[jArray.length()];
            test_rating = new Float[jArray.length()];
            songurl = new String[jArray.length()];
            viewss = new String[jArray.length()];

            for(int i=0;i<jArray.length();i++){
                json_data = jArray.getJSONObject(i);
                song_id[i]=json_data.getString("id");
                song_name[i]=json_data.getString("songname");
                artist[i]=json_data.getString("artist");
                displayname[i]=json_data.getString("displayname");
                description[i]=json_data.getString("description");
                genre[i]=json_data.getString("genre");
                custom_genre[i]=json_data.getString("customgenre");
                album[i]=json_data.getString("album");
                timestamp[i]=json_data.getString("format");
                avatar[i]=json_data.getString("image_url");
                songurl[i]=json_data.getString("song_url");
                //drawable[i] = LoadImageFromWebOperations(avatar[i]);
                test_rating[i] = (float) json_data.getDouble("rating");
                viewss[i] = json_data.getString("views");
                user5  = new UserRecord(genre[i], displayname[i], timestamp[i], drawable[i], test_rating[i], songurl[i], viewss[i], song_id[i], avatar[i]);

                publishProgress(user5);

            }
        }
        catch(JSONException e1){
            e1.printStackTrace();

        } catch (ParseException e1) {
            e1.printStackTrace();
        }           

        return null;
    }

    protected void onProgressUpdate(UserRecord... progress) {
        users.add(user5);

    }

    protected void onPostExecute(JSONArray jArray) {
        listView.setAdapter(new UserItemAdapter(mainmenu.this, android.R.layout.simple_list_item_1, users));
        dialog.dismiss();
    }

    protected void onPreExecute(){
        dialog = ProgressDialog.show(mainmenu.this, "", 
                "Loading. Please wait...", true);
    }
}

有人可以指出我正确的方向/我需要采取的步骤才能实现这一目标

2 个答案:

答案 0 :(得分:1)

看看CommonsWare EndlessAdapter

答案 1 :(得分:0)

恕我直言,您应该只更改光标的查询,这将限制所需的行。然后你的适配器可能是常见的。