句柄emptylist和asynctask

问题描述:

我使用有用的guide由Cyril Mottier写空白状态ListViewViewStub充气与编号@android:id/empty。在这种方法都很好,但如果我有ListActivity没有连接适配器通过setListAdapter或附加空适配器,然后我看到空视图。这一切都清楚,但如果我想实现这样的逻辑:句柄emptylist和asynctask

  1. 打开ListActivity
  2. 附加adapter
  3. 执行AcyncTaskAdapter获取数据,显示进度对话框
  4. 呼叫notifyDataSetChanged
  5. 解雇对话框

然后从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后我想显示文字与图像(例如没有发现)

+1

请考虑修改你的问题,也许添加一些代码。即时发现它很难理解 –

虽然我明白这是你想要达到什么目的?

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(); 
} 

}

+1

即使我不明白你为什么会在开始时看到ViewStub,当你改变可视性或者你明确地调用infalte时,它会被夸大。 – gwa

+0

这个评论是决定性的,我注意到如果'viewstub'将具有'android:id =“@android:id/empty”',它将自动在setContentView上可见。最后至少有一些动作。如果你按照你写的答案行事,那么一切正常。但膨胀的ViewStub涵盖了一切,即使数据不是空的。 – Mrusful

+0

很高兴帮助! – gwa

我建议你尝试设置空白字符串的文本值("")当AsyncTask完成时,将文本值更改为错误消息。

希望它解决了您的问题。