Android listview上没有显示数据
这是我的到达时间Android listview上没有显示数据,android,database,eclipse,sqlite,android-listview,Android,Database,Eclipse,Sqlite,Android Listview,这是我的到达时间 package one.two; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.database.Cursor; import android.os.Bundle; import android.widget.ArrayAdapter; import android.w
package one.two;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Arrival extends ListActivity
{
private ListView listView;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
ArrayList<String> retList = new ArrayList<String>();
System.out.println("Start onCreate Function\n");
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
System.out.println("In onCreate Function\n");
System.out.println("In of GetData\n");
DBAdapter db = new DBAdapter(this);
System.out.println("DB Open\n");
db.open();
System.out.println("DB Opened\n");
retList = getData();
System.out.println("Out of GetData\n");
// force count no. of records in table
// dump to check index
int cnt = 2;
int i=0;
for (i = 0; i<cnt; i++)
System.out.println(retList.toString());
System.out.println("Array 2 String\n");
Cursor c = db.getCursor();
String[] from = new String[] {DBAdapter.status};
int[] to = new int[] {R.id.txt1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.listtext, c, from, to);
this.setListAdapter(mAdapter);
System.out.println("Show List\n");
db.close();
}
public static ArrayList<String> getData()
{
ArrayList<String> items = DBAdapter.getAllTitles();
System.out.println("Return a LIST titles\n");
return items;
}
}
我可以知道为什么我的ListView上没有显示任何数据吗?在将数据填充到容器后,尝试手动调用适配器。您的代码中有两个错误,我现在可以发现。第一个应该不是问题。您正在创建两个ListAdapter并将它们都设置
setListAdapter(new SimpleCursorAdapter(this, android.R.id.list, c, from, to));
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.id.list, c, from, to);
this.setListAdapter(mAdapter);
下一件事是如何定义您的:
如文档中所示,SimpleCursorAdapter需要一个布局id,该id将用作列表中的单元格。您正在提供列表本身的id。适配器现在将在您的项目中查找与android列表具有相同id的布局文件,然后尝试扩大此布局,找到to数组中提到的视图,并将光标的值设置为该值。那是行不通的。至少在Logcat中应该有一个可见的错误
此外,您提供的to id不应位于主布局中,而应位于行布局本身中。如果这没有显示错误,您确定光标中有数据吗?好,我更改了SimpleCrsorAdapter的行。当前存在其他错误。不应在主线程上执行此操作。使用ContentProvider和Loader。。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ferry Hub"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ff009999"
android:layout_x="97px"
android:layout_y="15px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arrival Time"
android:layout_x="116px"
android:layout_y="48px"
>
</TextView>
<TextView
android:id="@+id/Text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_x="200px"
android:layout_y="90px"
>
</TextView>
<Button
android:id="@+id/widget36"
android:layout_width="96px"
android:layout_height="40px"
android:text="Back"
android:layout_x="101px"
android:layout_y="299px"
>
</Button>
<ListView
android:layout_width="wrap_content"
android:layout_y="132dip"
android:layout_x="150dip"
android:id="@android:id/list"
android:layout_height="wrap_content">
</ListView>
</AbsoluteLayout>
07-23 07:00:45.625: ERROR/AndroidRuntime(725): Uncaught handler: thread main exiting due to uncaught exception
07-23 07:00:45.654: ERROR/AndroidRuntime(725): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Departure}: java.lang.ClassCastException: java.util.ArrayList
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.os.Looper.loop(Looper.java:123)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread.main(ActivityThread.java:3948)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at java.lang.reflect.Method.invoke(Method.java:521)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at dalvik.system.NativeStart.main(Native Method)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): Caused by: java.lang.ClassCastException: java.util.ArrayList
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at one.two.Departure.onCreate(Departure.java:27)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
07-23 07:00:45.654: ERROR/AndroidRuntime(725): ... 11 more
setListAdapter(new SimpleCursorAdapter(this, android.R.id.list, c, from, to));
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.id.list, c, from, to);
this.setListAdapter(mAdapter);
new SimpleCursorAdapter(this, android.R.id.list, c, from, to)