【问题标题】:TextView keeps appearing behind ListViewTextView 不断出现在 ListView 后面
【发布时间】: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


    【解决方案1】:

    我没有正确理解您的问题......我得到的是当列表充满项目时,文本视图仍然可见。

    • 我认为您只是获得了 TextView 的参考

      TextView emptyView = (TextView) findViewById(R.id.empty);

    • 然后检查是否 (list.size > 0)

      使 textView 不可见

      emptyView.setVisibility(View.GONE);

    • 否则让它可见 setVisibility(View.VISIBLE);

    【讨论】:

    • 我实现了TextView emptyView = (TextView) findViewById(android.R.id.empty); if (array.length &gt; 0) { emptyView.setVisibility(View.GONE); } else { emptyView.setVisibility(View.VISIBLE); } 但无济于事。列表为空时的 TextView 会继续显示,即使列表已填充。
    【解决方案2】:

    如果您的列表不为空,您必须 setVisibility="gone" 到 textView,如果您的列表为空,则显示它。

    【讨论】:

      猜你喜欢
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 2015-03-13
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多