【问题标题】:Why is not populating the listView?为什么不填充 listView?
【发布时间】:2023-03-12 03:17:02
【问题描述】:

我正在尝试做一件非常简单的事情(显示填充的列表视图),但我做不到。我的代码不起作用,我找不到问题所在,所以我希望其他人可以帮助我:)

我定义了 listView 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10px">

<TextView
    android:id="@+id/descripcion"
    android:text="@string/descripcion"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"    />

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

</LinearLayout>

活动:

public class s_config extends Activity {

    public ArrayAdapter<String> lvAdapter;
    public ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.s_config); 

      final String[] datos = new String[]{"Elem1","Elem2","Elem3","Elem4","Elem5"};

      lvAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datos);
      lv = (ListView) findViewById(R.id.listaConfig);
      lv.setAdapter(lvAdapter);

    }
}

当我启动应用程序时,一切正常,显示 XML 的 TextView 但没有 ListView 的迹象...我错过了什么吗?

【问题讨论】:

    标签: android android-listview android-adapter


    【解决方案1】:

    在你的 Xml Layout 中有问题:

    将布局方向设置为垂直android:orientation="vertical",使您的列表视图可见。

    注意:

    如果您不设置方向,其值将默认为水平

    编辑布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10px"
       android:orientation="vertical">
    
    <TextView
        android:id="@+id/descripcion"
        android:text="@string/descripcion"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"    />
    
    <ListView
        android:id="@+id/listaConfig"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"    />
    
    </LinearLayout>
    

    【讨论】:

    • 哎哟!我知道我错过了一些类似的 st*pid 参数,但我自己无法找到 :) 也许应该用警告来建议这种事情?无论如何,非常感谢你:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多