【问题标题】:Showing items on a ListView在 ListView 上显示项目
【发布时间】:2016-05-07 15:19:05
【问题描述】:

我试图在 listView 上显示一些项目,但它没有显示在屏幕上。 这只是虚拟信息,但仍然无法正常工作。我试图查看一堆教程,但一无所获。

这是xml文件

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:fitsSystemWindows="true"
android:id="@+id/container"
tools:context="com.example.pedrofialho.musicristo.SearchActivity">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="55dp" />

</RelativeLayout>

这里是java代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_actvity);
    container = (ViewGroup) findViewById(R.id.container);
    LayoutInflater inflater = LayoutInflater.from(this);
    View rootView = inflater.inflate(R.layout.listview_search,container,false);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    list = (ListView) rootView.findViewById(R.id.list);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    String[] items = {"Comunhão","Passo a Passo","Tudo bem?","Esta lista não tem sentido"};
    mArrayList = new ArrayList<>(Arrays.asList(items));
    mArrayAdapter = new ArrayAdapter<>(this, R.layout.activity_search_actvity, R.id.list, mArrayList);
    list.setAdapter(mArrayAdapter);
}

在屏幕上它什么都不显示,只是一个白屏。 提前谢谢你

【问题讨论】:

  • 只是为了调试,尝试从 XML 中删除 ListView 并在其中放入一个 TextView,其中包含一些文本,看看是否可以看到。

标签: android listview


【解决方案1】:

Here 是 ArrayAdapter 初始化的正确方式。您必须提供一个包含例如的布局。一个TextView(如果你想显示文本),所以你应该将new ArrayAdapter... 的行修改为:mArrayAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, mArrayList);,因为现在你的适配器得到了你的活动的布局 em>,不是项目布局...
如果必须为每个项目使用自定义布局,则应传递其 id,以及 TextView 的 id。
如果您需要更复杂的项目视图,您可以扩展ArrayAdapterBaseAdapter 类来实现项目视图的创建。

【讨论】:

    【解决方案2】:
    View rootView = inflater.inflate(R.layout.listview_search,container,false);
    

    膨胀一个新的屏幕,直到你明确地把它放在屏幕上,甚至将它添加到当前视图层次结构的容器部分或替换当前内容(再次调用setContentView)。根据您发布的布局,我假设activity_search_actvity.xml 已经包含ListView,因此请去掉inflater,只需使用findViewById 来检索您使用setContentView(R.layout.activity_search_actvity); 设置的布局的对象部分。您还使用错误的参数实例化了ArrayAdapter。第二个参数是您要使用list 的项目的布局,第三个是您要用于显示数据集的TextViewid

    【讨论】:

      【解决方案3】:

      您将错误的参数传递给数组适配器。在您的情况下,它将是ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, mArrayList)

      详情ArrayAdapter in android to create simple listview

      在相对布局中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"

      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior>
      
          <ListView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/list"
              android:layout_alignParentTop="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_marginTop="55dp" />
      
      </RelativeLayout>
      

      正如support library guidelines:所解释的那样

      【讨论】:

        【解决方案4】:

        使用这个

         ArrayAdapter <String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.array);
        

        不要忘记将列表视图附加到它。

         ListView listView=getListView();//Your activity shoudl extends ListActivity
         listView.setAdapter(adapter);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多