我尝试使用数组运行一个简单的listview应用程序。我的程序没有错误,但是emulator中没有显示任何内容。显示屏幕是什么?
这是我的计划:
package com.example.lvdb;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity {
String [] values={"TAXI","FOOD","TICKET BOOKING","HOSPITAL"};
protected void oncreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lists=getListView();
lists = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lists.setAdapter(adapter);
}
}
答案 0 :(得分:3)
protected void oncreate(Bundle savedInstanceState) {..
ListView lists=getListView();
和lists = (ListView) findViewById(R.id.lv);
实际上是一回事(但你最好使用第一个)
package com.example.lvdb;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends ListActivity {
String [] values={"TAXI","FOOD","TICKET BOOKING","HOSPITAL"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values))
}
}
你的案子:
public class MyActivity extends ListActivity {
String [] values={"TAXI","FOOD","TICKET BOOKING","HOSPITAL"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lists=getListView();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lists.setAdapter(adapter);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
答案 1 :(得分:0)
首先检查列表的xml文件ID。它必须是@ + id / list
之后,获取列表并设置适配器。
代码:
ListView lists=getListView();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
setListAdapter(adapter);
必须在设置适配器之前设置值。