我有一个列表视图,用于从远程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);
}
}
有人可以指出正确的方向/步骤吗?
最佳答案
恕我直言,您应该只更改游标的查询,这会将行限制为所需的行。那么您的适配器可能是常见的适配器。
关于android - 无尽的ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9167228/