Nic*_*ick 5 android json listview asynchronous
我从我的服务器上的PHP脚本中获取json对象生成,然后使用图像的延迟加载将其解析为listview.问题是json将加载相对较快,或者它会挂起一两秒,具体取决于服务器上的响应,这可能令人沮丧.当我第一次打开应用程序时,挂起特别令人讨厌,因为它将挂在黑色屏幕上直到对象已加载,然后当我在应用程序中更新列表时它具有相同但至少视图已加载.这是我获取json的代码:
public void getJson(String selection, String url) {
JSONObject json = null;
String formatedcat = selection.toLowerCase();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
json = JSONfunctions
.getJSONfromURL(url);
try {
//the array title that you parse
JSONArray category = json.getJSONArray(formatedcat);
for (int i = 0; i < category.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = category.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
c.getString("title"));
//map.put("text",c.getString("title"));
map.put("ts",c.getString("run_date") );
map.put("image","http:"+c.getString("url"));
mylist.add(map);
}
} catch (JSONException e) {
}
ListAdapter adapter = new JsonAdapter(this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
适配器代码:
public class JsonAdapter extends SimpleAdapter {
public ImageManager imageManager;
public ListActivity context;
public ArrayList<HashMap<String,String>> list;
public String[] fieldNames;
public int[] fieldTargetIds;
public JsonAdapter(ListActivity c,
ArrayList<HashMap<String, String>> mylist,
int textViewResourceId,
String[] fieldNames,
int[] fieldTargetIds) {
super(c, mylist, textViewResourceId, fieldNames, fieldTargetIds );
this.context = c;
this.list = mylist;
this.fieldNames = fieldNames;
this.fieldTargetIds = fieldTargetIds;
this.imageManager = new ImageManager(context.getApplicationContext());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = vi.inflate(R.layout.list, null);
}
//super.getView(position, convertView, parent);
ImageView imgView = (ImageView) row.findViewById(R.id.doodlepic);
try {
String url = list.get(position).get("image");
imgView.setTag(url);
imageManager.displayImage(url, context, imgView);
} catch (Exception e) {
}
for (int i=0; i<fieldNames.length; i++) {
TextView tv = (TextView) row.findViewById(fieldTargetIds[i]);
tv.setText(list.get(position).get(fieldNames[i]));
}
return row;
}
}
Run Code Online (Sandbox Code Playgroud)
在生成和下载json对象时,如何实现异步任务以显示进度对话框?我尝试过使用线程和异步任务但我无法弄清楚如何将代码拆分为适当的部分.
Jam*_*Lee 17
asPreExecute,AsyncTask的onPostExecute将在UI线程中运行,doInBackground将在另一个线程中运行,所以下面的代码应该对你好
public class YourActivity extends Activiy{
public void getJson(String selection, String url) {
new LoadJsonTask().execute( selection, url);
}
private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
ProgressDialog dialog ;
protected void onPreExecute (){
dialog = ProgressDialog.show(YourActivity.this ,"title","message");
}
protected ArrayList<HashMap<String, String>> doInBackground (String... params){
return doGetJson(params[0],params[1]);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> mylist){
ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
dialog.dismiss();
}
}
public ArrayList<HashMap<String, String>> doGetJson(String selection, String url) {
JSONObject json = null;
String formatedcat = selection.toLowerCase();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
json = JSONfunctions
.getJSONfromURL(url);
try {
//the array title that you parse
JSONArray category = json.getJSONArray(formatedcat);
for (int i = 0; i < category.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = category.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
c.getString("title"));
//map.put("text",c.getString("title"));
map.put("ts",c.getString("run_date") );
map.put("image","http:"+c.getString("url"));
mylist.add(map);
}
} catch (JSONException e) {
}
return mylist;
....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14210 次 |
| 最近记录: |