将预填充的数据库显示为列表视图

时间:2014-12-02 06:19:51

标签: java android xml listview

我正在尝试在listview中显示数据库的多个列。我正在使用sqliteasset帮助程序和row.xml和main.xml。我的主动性延伸了一个列表活动。我得到的错误是findviewbyid无法使用" R.id.list"找到线性布局。但我的activity_main.xml有它。

这是我的主类,它在列表中显示数据库信息。

public class MainActivity extends ListActivity {

    private Cursor schedule;
    private MyDatabase db;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new MyDatabase(this);
        schedule = db.getSchedule();

        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(new SimpleCursorAdapter(
           this,  // The context
           R.layout.row,  // The layout of the row to show
           db.getSchedule(),  // Here the cursor where the data is
                               // but it can be anything
           new String[] {"fName", "fType" },  // here the tables to show
           new int[] { android.R.id.text1, android.R.id.text2 },  // here where to show? the IDs
                                             // of the layout elements where put data.
           0));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        schedule.close();
        db.close();
    }
}

行:          

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

</LinearLayout>

Activity_Main :(你可以看到id.list)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView 
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>

logcat的:

12-02 01:04:44.436: E/AndroidRuntime(1183): FATAL EXCEPTION: main
12-02 01:04:44.436: E/AndroidRuntime(1183): Process: com.example.mealplan, PID: 1183
12-02 01:04:44.436: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:136)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.setContentView(Activity.java:1929)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.example.mealplan.MainActivity.onCreate(MainActivity.java:35)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5231)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

2 个答案:

答案 0 :(得分:0)

更改此行:

android:id="@+id/list"

android:id="@id/android:list"

答案 1 :(得分:0)

您正在尝试从Android默认库中获取列表ID。您应该从自己的布局中检索列表ID。

更改此行

ListView listView = (ListView) findViewById(android.R.id.list);

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