句柄emptylist和asynctask
我使用有用的guide由Cyril Mottier写空白状态ListView
在ViewStub
充气与编号@android:id/empty
。在这种方法都很好,但如果我有ListActivity
没有连接适配器通过setListAdapter
或附加空适配器,然后我看到空视图。这一切都清楚,但如果我想实现这样的逻辑:句柄emptylist和asynctask
- 打开
ListActivity
- 附加
adapter
- 执行
AcyncTask
和Adapter
获取数据,显示进度对话框 - 呼叫notifyDataSetChanged
- 解雇对话框
然后从1到3步,我看起来空虚y查看错误(例如“未找到”)。但是我只想在执行AsyncTask
后才显示错误。在执行AsyncTask
期间,我只想显示进程对话框。我尝试在@android:id/empty
附近使用可见性选项,尝试在xml
中使用include
。但是不能实现这个逻辑。任何建议,将不胜感激。
UPD:更多信息 我有activity.xml
:
<FrameLayout
android:id="@id/gd_action_bar_content_view"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="@android:id/list"
style="@style/listViewStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false" />
<ViewStub
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="@layout/error" />
</FrameLayout>
error.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/ic_cheese" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textStyle="bold"
android:textSize="16sp"
android:text="@string/no_cheese" />
</LinearLayout>
ListActivity类:
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
和异步类:
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
@Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
@Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
dialog.dismiss();
showResult(result); //this is fill adapter data and call notifyDataSetChanged
}
}
当活动创建和膨胀activity.xml
它显示空带,然后执行AsyncTask
,而AsyncTask
执行我看空的观点。当调用方法AsyncTask
我看到HTTP GET请求的结果。当活动创建和膨胀activity.xml
它显示为空,然后执行AsyncTask
,而AsyncTask
执行我看到空视图。当调用方法AsyncTask
我看到HTTP GET请求的结果。我认为这是错误的。我希望显示空白列表视图执行AsyncTask之前,如果结果等于Collections.EMPTY_LIST后我想显示文字与图像(例如没有发现)
虽然我明白这是你想要达到什么目的?
public class MyListActivity extends ListActivity implements OnScrollListener, OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity); //inflate activity with ViewStub
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.GONE);
mAdapter = new MyAdapter(this, Collections.EMPTY_LIST);
setListAdapter(mAdapter);
new SearchAndFill().execute("");
}
}
... ... ....
private class SearchAndFill extends AsyncTask<String, Void, List<Object>>{
LoadingDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
if (dialog==null){
dialog = new LoadingDialog(MyListActivity.this);
dialog.setCancelable(false);
}
dialog.show();
}
@Override
protected List<Object> doInBackground(String... str) {
return search(str[0]); //this is HTTP GET request
}
@Override
protected void onPostExecute(List<Object> result) {
super.onPostExecute(result);
if (result == null || result.size() == 0){
mAdapter.notifyDataSetInvalidated();
((ViewStub) findViewById(R.id.YOUR_VIEW_STUB_ID)).setVisibility(View.VISIBLE);
} else {
// add stuff to mAdapter here...
mAdapter.notifyDataSetChanged();
}
dialog.dismiss();
}
}
我建议你尝试设置空白字符串的文本值(""
)当AsyncTask完成时,将文本值更改为错误消息。
希望它解决了您的问题。
请考虑修改你的问题,也许添加一些代码。即时发现它很难理解 –