【发布时间】:2016-11-30 16:25:35
【问题描述】:
我在以前的答案中一直在查看 ListViews,但我似乎无法找到解决问题的方法。首先,尽管从 SQLite 数据库中成功检索了一组数据,但即使成功填充列表,没有数据时的 TextView 仍然可见。
我正在尝试在历史活动中显示以前的用户输入,如下所示:
HistoryActivity.java
public class HistoryActivity extends ListActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
SimpleCursorAdapter mAdapter;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
// progress bar
// Create a progress bar to display while the list loads
ProgressBar progressBar = new ProgressBar(this);
progressBar.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
progressBar.setIndeterminate(true);
getListView().setEmptyView(progressBar);
// Must add the progress bar to the root of the layout
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
root.addView(progressBar);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
array);
setListAdapter(arrayAdapter);
}
activity_history.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:layout_weight="1"/>
<TextView
android:text="Nothing saved. Try to answer some questions now!"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:id="@android:id/empty"
/>
</RelativeLayout>
根据之前的 SO 答案,我确保 .xml 文件中的 id 是正确的,据我了解,在扩展 ListActivity 时,您不需要使用 .setEmptyView()。
我可能错过了一些基本的东西,因此我们将不胜感激。
提前致谢!
【问题讨论】:
标签: android listview textview android-arrayadapter listadapter