我想使用下面的代码生成列表视图。但是在运行此代码后,屏幕只显示一个空白屏幕,并在主布局中显示“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>
答案 0 :(得分:2)
您的ListView in XML应具有此android:id="@android:id/list"
并删除该背景属性..
答案 1 :(得分:0)
您正在将适配器设置为当前活动,但它的视图不是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));
答案 2 :(得分:0)
答案 3 :(得分:0)
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"
默认情况下,列表文本的颜色为白色 并且您已经列出了白色背景以便列出项目