列表视图在加载数据后没有显示,但是当屏幕显示关闭并且显示列表时。不知道发生了什么。我正在使用xiamoi mi手机来测试应用程序,如果任何其他活动代码只需要让我知道
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class FriendList extends ListActivity {
String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
email = bundle.getString("email");
setContentView(R.layout.contacts_list);
final List<Model> list = new ArrayList<Model>();
/** This block is for getting the image url to download from the server **/
final GetDataFromDB getvalues = new GetDataFromDB();
final ProgressDialog dialog = ProgressDialog.show(FriendList.this,
"", "Gettting values from DB", true);
new Thread (new Runnable() {
public void run() {
String response = getvalues.getImageURLAndDesciptionFromDB(email);
System.out.println("Response : " + response);
if (!response.equalsIgnoreCase("")) {
if (!response.equalsIgnoreCase("error")) {
dismissDialog(dialog);
// Got the response, now split it to get the image Urls and description
String all[] = response.split("\\|::endline::\\|");
for(int k = 0; k < all.length; k++){
String urls_and_desc[] = all[k].split("\\|::break::\\|"); // urls_and_desc[0] contains image url and [1] -> description
System.out.println("image url : " + urls_and_desc[2]);
list.add(get(urls_and_desc[1], "https://xxxx.xx/" + urls_and_desc[2]));
}
}
} else {
dismissDialog(dialog);
}
}
}).start();
/*************************** GOT data from Server ********************************************/
ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
public void dismissDialog(final ProgressDialog dialog){
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
}
private Model get(String s, String url) {
return new Model(s, url);
}
}
答案 0 :(得分:0)
从服务器获取数据后,您应该调用((ArrayAdapter<Model>)getListAdapter()).notifyDataSetChanged()
:
new Thread (new Runnable() {
public void run() {
String response = getvalues.getImageURLAndDesciptionFromDB(email);
System.out.println("Response : " + response);
if (!response.equalsIgnoreCase("")) {
if (!response.equalsIgnoreCase("error")) {
dismissDialog(dialog);
// Got the response, now split it to get the image Urls and description
String all[] = response.split("\\|::endline::\\|");
for(int k = 0; k < all.length; k++){
String urls_and_desc[] = all[k].split("\\|::break::\\|"); // urls_and_desc[0] contains image url and [1] -> description
System.out.println("image url : " + urls_and_desc[2]);
list.add(get(urls_and_desc[1], "https://xxxx.xx/" + urls_and_desc[2]));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
((ArrayAdapter<Model>)getListAdapter()).notifyDataSetChanged();//Notify ListView to update itself.
}
});
}
} else {
dismissDialog(dialog);
}
}
}).start();