为什么ListView中没有显示任何内容,尽管没有错误?

时间:2017-05-26 07:27:22

标签: java android xml android-layout

我寻找解决方案,但我无法正确理解,为什么会发生这种情况。我试图将列表视图放在线性布局中,但它没有帮助.C任何人都可以帮助我查看我的列表视图。

这是我的Xml - >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_cards"
android:orientation="vertical">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Card - 1"
        android:textAlignment="center"
        android:textColor="@color/crad_color"
        android:textSize="34sp"

        />
</RelativeLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_num"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAlignment="center">

    </ListView>
</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_icon">


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"

            >


            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?actionBarItemBackground">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="16dp"
                    android:text="Yes its there !" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="?actionBarItemBackground"

                >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="16dp"
                    android:text="NO ! its not"

                    />

            </FrameLayout>

        </RelativeLayout>

    </RelativeLayout>

</LinearLayout>


 </LinearLayout>

这是我的java代码 - &gt;

package com.example.android.chooseanumber_magicalapp;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import static android.R.attr.id;
import static android.R.id.list;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static com.example.android.chooseanumber_magicalapp.R.id.list_num;

 /**
  * Created by great on 25-05-2017.
*/

public class InstructionActivity extends AppCompatActivity{

ArrayList<String> list = new ArrayList<String>(30);

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


    list.add("1");
    list.add("1");
    list.add("1");
    list.add("1");
    list.add("1");
    list.add("1");
    list.add("1");


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);

    ListView lv = (ListView) findViewById(list_num);
    {
        lv.setAdapter(adapter);
    }

}

 }

编辑 - &gt;

来自android studio doesn't show anything in editor 的图片

从手机拍摄的屏幕截图 - &gt;

编辑__&gt;

好吧,伙计们,如果我将我的风格改为

,我就会遇到问题
  <resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

显示列表项!

之前

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

但如果我想要一个全屏(没有操作栏)怎么办?

5 个答案:

答案 0 :(得分:0)

问题在于listview转换。更改以下行

  ListView lv = (ListView) findViewById(list_num);

 ListView lv = (ListView) findViewById(R.id.list_num);

答案 1 :(得分:0)

试试这个,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
ListView lv = (ListView) findViewById(R.id.list_num);
lv.setAdapter(adapter);

答案 2 :(得分:0)

更改代码中的这些行

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);

    ListView lv = (ListView) findViewById(R.id.list_num);
    lv.setAdapter(adapter);

答案 3 :(得分:0)

//1. Create arr (data source)
 final String arr[]={"Teo","Ty","Bin","Bo"};
 //2. get listview by id
 ListView lv=(ListView) findViewById(R.id.lvperson);
 //3. put the data
 ArrayAdapter<String>adapter=new ArrayAdapter<String>
 (this, android.R.layout.simple_list_item_1, arr);
 //4. put it in listview
 lv.setAdapter(adapter);

如果它不起作用,只用listview进行测试!

答案 4 :(得分:0)

它的工作只将findviewbyID(list_item)改为findviewbyID(R.id.list_item)enter image description here