簡體   English   中英

ListView沒有填充

[英]ListView is not getting populated

我想使用下面的代碼生成列表視圖。 但是在運行此代碼之后,屏幕只顯示一個空白屏幕,並在主布局中顯示“hello”文本

package com.android.test1;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Toast;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;

public class HelloListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        String a[] = new String[]{"a", "b", "c", "d"};
         setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));

          ListView lv = getListView();

          lv.setTextFilterEnabled(true);

    }
}

主要布局代碼如下

<?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:orientation="vertical" >
    <TextView 
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF">
</ListView>

</LinearLayout>

你在XML中的ListView應該有這個android:id="@android:id/list"

並刪除該背景屬性..

Archie.bpgc參見本教程http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php#ListView

如果擴展Listactivity,則不添加

的setContentView(R.layout.main);

並參考它

ListView listView = (ListView) findViewById(R.id.list_view);

如果你這樣做的話

public class Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        ListView listView = (ListView) findViewById(R.id.list_view);
        String a[] = new String[]{"a", "b", "c", "d"};
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                android.R.id.text1 ,

                a));

//          ListView lv = getListView();

        listView.setTextFilterEnabled(true);

    }
}

你已經設置了列表背景

android:background="#FFFFFFFF"

默認情況下,列表文本的顏色為白色,並且您已指定白色背景以便列出,因此不會顯示項目

您正在將適配器設置為當前活動,但它的視圖不是ListView而是LinearLayout。 嘗試此操作將適配器設置為正確的視圖。

String a[] = new String[]{"a", "b", "c", "d"};
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));

在您的代碼中只需注釋以下行

的setContentView(R.layout.main);

在這種情況下,請使用以下鏈接來使用自定義適配器。

鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM