我正在使用doInBackground部分中的代码从数据库加载自定义列表视图,现在我已将其添加到AsynchTask,它在对话框被解除时不显示任何内容。有没有人知道我做错了什么,以便当进度条再次解散时它会显示我的列表:
private class LoadList extends AsyncTask<String, UserRecord, JSONArray> {
protected JSONArray doInBackground(String... link) {
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://wallpaperapp.x10.mx/new.php");
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 int[jArray.length()];
avatar = new String[jArray.length()];
drawable = new Drawable[jArray.length()];
test_rating = new Float[jArray.length()];
songurl = new String[jArray.length()];
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
song_id[i]=json_data.getInt("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");
user5 = new UserRecord(genre[i], displayname[i], timestamp[i], drawable[i], test_rating[i], songurl[i]);
publishProgress(user5);
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No results found." ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
return null;
}
protected void onProgressUpdate(UserRecord... progress) {
users.add(user5);
}
protected void onPostExecute(JSONArray jArray) {
dialog.dismiss();
}
protected void onPreExecute(){
dialog = ProgressDialog.show(mainmenu.this, "",
"Loading. Please wait...", true);
}
}
这是我的onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsmain);
nameValuePairs = new ArrayList<NameValuePair>();
users = new ArrayList<UserRecord>();
ListView listView = (ListView) findViewById(R.id.ListViewId);
songname = (TextView) findViewById(R.id.tvTrack);
songimage = (ImageView) findViewById (R.id.imageView1);
playbutton = (Button) findViewById (R.id.playbutton1);
downloadbutton = (Button) findViewById (R.id.downloadbutton3);
listView.setAdapter(new UserItemAdapter(this, android.R.layout.simple_list_item_1, users));
flipper = (ViewFlipper) findViewById(R.id.tab2);
//when a view is displayed
flipper.setInAnimation(this,android.R.anim.fade_in);
//when a view disappears
flipper.setOutAnimation(this, android.R.anim.fade_out);
screenWidth = getResources().getDisplayMetrics().widthPixels;
new LoadList().execute();
}
即将发布的UserRecord:
public class UserRecord {
public String genrez;
public String displaynamez;
public String timestampz;
public Drawable image_urlz;
public Float test_ratingz;
public String songurlz;
public UserRecord(String genrez, String displaynamez, String timestampz, Drawable image_urlz, Float test_rating, String songurlz) {
this.genrez = genrez;
this.displaynamez = displaynamez;
this.timestampz = timestampz;
this.image_urlz = image_urlz;
this.test_ratingz = test_rating;
this.songurlz = songurlz;
}
}
答案 0 :(得分:3)
将此添加到我的onPostExecute工作了!我很开心我想出来了..谢谢
listView.setAdapter(new UserItemAdapter(mainmenu.this, android.R.layout.simple_list_item_1, users));
答案 1 :(得分:0)
@Override
protected void onPostExecute(String result) {
dialog.dismiss();
}
请将listview代码放在此函数中,表示将数据绑定到listview