异步任务完成后更新列表视图
异步任务完成后如何更新 listivew。以下是示例代码,但列表视图未更新。
class CallXML extends AsyncTask<Void, Void, Void> {
int gcid;
int scid;
public CallXML(int gid, int sid) {
// TODO Auto-generated constructor stub
gcid = gid;
scid = sid;
}
protected void onPreExecute() {
}
protected Void doInBackground(Void... arg0) {
// here goes the xml parsing....
}
return null;
}
protected void onPostExecute(String result) {
Log.e("TAG", "In postExecute");
Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null);
cur3.moveToFirst();
do {
quotesarray.add(cur3.getString(2));
} while (cur3.moveToNext());
if(cur3 != null){
cur3.close();
}
QList.post(new Runnable() {
public void run() {
mAdapter = new CustomAdapter();
mAdapter.notifyDataSetChanged();
QList.setAdapter(mAdapter);
}
});
if (helper2 != null) {
helper2.close();
}
if (database2 != null) {
database2.close();
}
}
}
编辑:
实际上 onPostExecute
没有执行,为什么..这是我调用 asynctask new CallXML(gcid, scid).execute();
的方式
How do i update listivew when async task is done. Below is the sample code but the listview isn't updated.
class CallXML extends AsyncTask<Void, Void, Void> {
int gcid;
int scid;
public CallXML(int gid, int sid) {
// TODO Auto-generated constructor stub
gcid = gid;
scid = sid;
}
protected void onPreExecute() {
}
protected Void doInBackground(Void... arg0) {
// here goes the xml parsing....
}
return null;
}
protected void onPostExecute(String result) {
Log.e("TAG", "In postExecute");
Cursor cur3 = database2.query("Quote", qfield, null, null, null, null, null);
cur3.moveToFirst();
do {
quotesarray.add(cur3.getString(2));
} while (cur3.moveToNext());
if(cur3 != null){
cur3.close();
}
QList.post(new Runnable() {
public void run() {
mAdapter = new CustomAdapter();
mAdapter.notifyDataSetChanged();
QList.setAdapter(mAdapter);
}
});
if (helper2 != null) {
helper2.close();
}
if (database2 != null) {
database2.close();
}
}
}
EDIT:
Acutally onPostExecute
is not executed why..This is the way I call asynctask new CallXML(gcid, scid).execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此外,
onPostExecute
位于主线程上,因此不应在那里进行数据库查询。相反,在doInBackground
中获取数据并从那里返回最终集合。onPostExecute
可用于 UI 更新并通过结果集合更新适配器。编辑:
不需要发布可运行的文件,因为您处于主循环中。
Also,
onPostExecute
is on the main thread so should not be doing database queries there. Instead, get data indoInBackground
and return the final collection from there.onPostExecute
can be used for UI updates and updating your adapter with result collection.Edit: posting a runnable
is not required since you are in the main loop.
您没有在代码中向适配器提供字符串项。当你将适配器设置为列表时,你不需要调用notifyDataSetChanged,因为当你设置适配器时,它会自动将数据加载到列表中。也许你我尝试这样做:
You are not providing string items to the adapter in your code. And you don't need to call notifyDataSetChanged when you are setting adapter to a list, because when you set an adapter, it automatically loads the data into list. Perhaps you me try doing it in this way:
你有什么错误吗?如果是这样,请发布。如果没有检查从数据库获取的数据大小,如果您想刷新列表视图,只需调用 listview.invalidateViews() 它将刷新列表视图并在列表视图中设置新数据。
Did u got any error? if so please post it. If not Check the size of data you got from database and if you want to refresh listview just call listview.invalidateViews() it will refresh the list view and set the new data in the listview.