【发布时间】:2013-06-01 16:26:27
【问题描述】:
我正在开发一个应该从 web 服务获取 JSON 响应并将每个元素写入 listview 的应用程序,我已经读到我应该使用 AsyncTask获取 HTTP 响应,我做到了,我可以从 web 服务中检索数据并将它们显示在 TextViews 中。但是当我尝试在列表视图中显示元素时,它不会显示 anything 并在 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;
}
}
那么,我做错了什么?你对此有什么想法吗?
谢谢。
【问题讨论】:
-
这不是一个解决方案,而是一个测试,它可能有助于找出问题出在哪里。那个 for 循环可能是问题所在,请尝试使用 1 之类的数字而不是
records.count(),看看是否仍然出现错误,只需尝试排除可能出现问题的每个区域。 -
让我知道发生了什么...或者您可以输出记录的长度,如果它太长,请尝试在将其带到主线程之前在后台线程上进行处理...跨度>
-
谢谢。它不显示任何内容,但给了我
06-05 21:27:32.367: I/dalvikvm-heap(23426): Grow heap (frag case) to 8.924MB for 691216-byte allocation -
很好,说明问题出在循环上,试试下面的答案
-
好的,所以我意识到问题在于您将适配器设置在该循环中,因此您无法将其移动到后台线程,请给我一分钟将一些代码放在一起...
标签: android android-listview android-asynctask baseadapter