我正在开发一个应该从 web 服务获取 JSON 响应并在listview中写入每个元素的应用程序,我已经读到我应该使用AsyncTask来获取 HTTP 响应并且我做到了,我可以从 web 服务中检索数据并将它们显示在TextViews中。但是当我尝试在列表视图中显示元素时,它不会显示任何内容,并在 logcat 中给我以下消息:06-05 19:44:27.418: I/Choreographer(20731): Skipped 60 frames! The application may be doing too much work on its main thread.
这是我的主要代码:
public class MainActivity extends Activity {
private static JsonObject response = new JsonObject();
private ArrayList<SearchResults> results = new ArrayList<SearchResults>();
private SearchResults sr1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LoginAction().execute("");
ArrayList<SearchResults> searchResults = results;
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class LoginAction extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
try {
response = EventPulseCloud.call("ListEvents", callArgs);
} catch (HttpClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
return response.get("Type").toString();
}
protected void onPostExecute(String result) {
if(result.equals("success")) {
JsonArray records = null;
try {
records = response.getObject ("Data").getArray ("Records");
} catch (JsonException e) {
e.printStackTrace();
}
for(int i = 0; i < records.count(); i++) {
JsonObject record = (JsonObject) records.get(i);
sr1 = new SearchResults();
sr1.setAddress(record.get("address").toString());
results.add(sr1);
}
}
}
}
}
我的列表适配器:
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtAddress = (TextView) convertView.findViewById(R.id.address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtAddress.setText(searchArrayList.get(position).getAddress());
return convertView;
}
static class ViewHolder {
TextView txtAddress;
}
}
最后是 SearchResults.java :
public class SearchResults {
private String address = "";
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
那么,我做错了什么?你对此有什么想法吗?
谢谢你。