ArrayAdapter未显示在ListView - Android Studio中

时间:2016-07-10 15:07:22

标签: android listview android-arrayadapter

简单的问题。 ListView出现在我的Android Studio预览版中,但是当我运行应用程序时,它只是空白。

我有一个arrayadapter,它从数组中提取一些数据,然后将其推送到活动中的ListView中。它应该显示用户可以在活动中滚动的菜单项列表。没有这样的运气。

我尝试使用XML文件的一些功能,但没有修复,所以我在这里。

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.ericleeconklin.sleepnoise2.ListSounds"
tools:showIn="@layout/activity_list_sounds">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context=".ListActivity">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ListView>

</LinearLayout>

这是我的Java

package com.ericleeconklin.sleepnoise2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListSounds extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_sounds);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

public class ListDisplay extends Activity {
    // Array of strings...
    String[] mobileArray = {"Waterfall","Fire","Fan","River","Heavy `Rain","Light Rain","Lawnmower","Restaurant"};`

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_list_sounds);

        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);

        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);
    }
}

}

2 个答案:

答案 0 :(得分:0)

将您的代码修改为以下内容:

package com.ericleeconklin.sleepnoise2;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListSounds extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_sounds);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        // Array of strings...
        String[] mobileArray = {"Waterfall","Fire","Fan","River","Heavy `Rain","Light Rain","Lawnmower","Restaurant"};
        ArrayAdapter adapter = new ArrayAdapter<String>(this,  R.layout.activity_listview, mobileArray);   
        ListView listView = (ListView) findViewById(R.id.mobile_list);
        listView.setAdapter(adapter);

    }

}

答案 1 :(得分:0)

在对ArrayAdapter构造函数的调用中,资源id应该是包含要在实例化视图时使用的TextView的布局文件的资源ID。

您可以尝试使用

android.R.layout.activity_list_item 

而不是

R.layout.activity_listview

你可能也想看看这个: ArrayAdapter in android to create simple listview