0

我的问题是我不知道如何准确地显示数据库中的数据。

这是我的 DatabaseHelper 活动(我已经删除了不需要的代码),它打开/关闭数据库并创建表。

final static String PN_TABLE = "Tablica_Poniedzialek";
    static final String PN_KEY_ID = "_id";
    final static String PN_KEY_TYPE_OF_SESSION ="type_of_session";
    final static String PN_KEY_START_TIME ="start_time";
    final static String PN_KEY_END_TIME ="end_time";
    final static String PN_KEY_NAME ="name";                         
    final static String PN_KEY_ROOM ="room";  

创建和添加数据到数据库工作正常。问题是我不知道应该如何准确地显示插入数据库的数据。我尝试过使用游标和列表适配器,但似乎我搞砸了一些事情,因为每次我在模拟器中启动它时都会强制关闭应用程序。

这是我的活动,负责从数据库中获取数据然后显示它。我不知道如何将数据显示为列表视图。我不确定哪种方法会更好,一种是创建列表(我相信它确实如此),还是一种只是从数据库中获取数据。但是,假设第一个是正确的,那么问题是我如何才能真正显示所有数据?

    package com.projekt;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;


public class PoniedzialekActivity extends ListActivity implements OnClickListener{

    private Button butPnAdd;
    private DatabaseHelper dbhelper;
    private SQLiteDatabase db; 
    Cursor cursor;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_poniedzialek);

        butPnAdd = (Button) findViewById(R.id.butPnAdd);
        butPnAdd.setOnClickListener(this);

        List<Poniedzialek> lista_poniedzialek = getAllPnSession();
        this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, lista_poniedzialek)); 



    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.butPnAdd){
        Intent i = new Intent(PoniedzialekActivity.this,dodawaniePoniedzialek.class);
        startActivity(i);
        }
    }
    public List<Poniedzialek> getAllPnSession() {
        List<Poniedzialek> pnsessionList = new ArrayList<Poniedzialek>();
        // Wybierz cale Query
        String selectQuery = "SELECT  * FROM " + DatabaseHelper.PN_TABLE;
        dbhelper.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // petla po wszystkich rzedach & dodawanie ich do listy
        if (cursor.moveToFirst()) {
            do {
                Poniedzialek pn = new Poniedzialek();

                pn.setID(Integer.parseInt(cursor.getString(0)));
                pn.setTypeOfSession(cursor.getString(1));
                pn.setName(cursor.getString(2));
                pn.setStartTime(cursor.getString(3));
                pn.setEndTime(cursor.getString(4));
                pn.setRoom(cursor.getString(5));

                // Dodawanie sesji do listy
                pnsessionList.add(pn);
            } while (cursor.moveToNext());
        }
        cursor.close();
        // return contact list
        return pnsessionList;
    }
    public Cursor fetchPn() {
        SQLiteDatabase db = dbhelper.getWritableDatabase(); 
        return db.query(DatabaseHelper.PN_TABLE, new String[] {DatabaseHelper.PN_KEY_TYPE_OF_SESSION, DatabaseHelper.PN_KEY_START_TIME,
                DatabaseHelper.PN_KEY_END_TIME, DatabaseHelper.PN_KEY_NAME, DatabaseHelper.PN_KEY_ROOM}, null, null, null, null, null);
    }

} // end PoniedzialekActivity

这是 layout_poniedzialek:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/butPnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_pn"
        />
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/tab1"
        />



    <ListView
        android:id="@+id/pnListView"
        android:layout_width="304dp"
        android:layout_height="380dp"
        android:layout_gravity="center_horizontal" >

    </ListView>

</LinearLayout>

编辑 现在我不断收到错误:NullPointerException。这是我在 poniedzialekActivity 中实现的(请注意,DatabaseHelper 是我保持打开/关闭和静态最终字符串属性的类的名称 - 我在这里创建 5 个平板电脑)

PoniedzialekActivity类:

public class PoniedzialekActivity extends ListActivity implements OnClickListener{

    private Button butPnAdd;
    private DatabaseHelper dbhelper;
    private SQLiteDatabase db; 
    Cursor c;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_poniedzialek);

        butPnAdd = (Button) findViewById(R.id.butPnAdd);
        butPnAdd.setOnClickListener(this);


        dbhelper = new DatabaseHelper(this);
        dbhelper.open();
        c = dbhelper.fetchPn();
        String[] columns = new String[] { DatabaseHelper.PN_KEY_TYPE_OF_SESSION, DatabaseHelper.PN_KEY_START_TIME, DatabaseHelper.PN_KEY_END_TIME,
                DatabaseHelper.PN_KEY_NAME, DatabaseHelper.PN_KEY_ROOM};
        int[] to = new int[] { R.id.textSession, R.id.textStartTime, R.id.textEndTime, R.id.textName, R.id.textRoom };
        new SimpleCursorAdapter(PoniedzialekActivity.this, R.layout.entry, c, columns, to);
        ListView list = getListView();
        list.setAdapter(new SimpleCursorAdapter(PoniedzialekActivity.this, R.layout.entry, c, columns, to));

    }
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.butPnAdd){
        Intent i = new Intent(PoniedzialekActivity.this,dodawaniePoniedzialek.class);
        startActivity(i);
        }
    }
}

DatabaseHelper.class

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;
    public static final String TAG = "ProjectDatabase";

    static final String PN_TABLE = "Tablica_Poniedzialek";
    static final String PN_KEY_ID = "_id";
    static final String PN_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String PN_KEY_START_TIME ="start_time";
    static final String PN_KEY_END_TIME ="end_time";
    static final String PN_KEY_NAME ="name";                         
    static final String PN_KEY_ROOM ="room";

    static final String WT_TABLE = "Tablica_Wtorek";
    static final String WT_KEY_ID = "_id";
    static final String WT_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String WT_KEY_START_TIME ="start_time";
    static final String WT_KEY_END_TIME ="end_time";
    static final String WT_KEY_NAME ="name";
    static final String WT_KEY_ROOM ="room";

    static final String SR_TABLE = "Tablica_Sroda";
    static final String SR_KEY_ID = "_id";
    static final String SR_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String SR_KEY_START_TIME ="start_time";
    static final String SR_KEY_END_TIME ="end_time";
    static final String SR_KEY_NAME ="name";
    static final String SR_KEY_ROOM ="room";

    static final String CZ_TABLE = "Tablica_Czwartek";
    static final String CZ_KEY_ID = "_id";
    static final String CZ_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String CZ_KEY_START_TIME ="start_time";
    static final String CZ_KEY_END_TIME ="end_time";
    static final String CZ_KEY_NAME ="name";
    static final String CZ_KEY_ROOM ="room";

    static final String PT_TABLE = "Tablica_Piatek";
    static final String PT_KEY_ID = "_id";
    static final String PT_KEY_TYPE_OF_SESSION ="type_of_session";
    static final String PT_KEY_START_TIME ="start_time";
    static final String PT_KEY_END_TIME ="end_time";
    static final String PT_KEY_NAME ="name";
    static final String PT_KEY_ROOM ="room";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }           
    private DatabaseHelper dbhelper; 
    private SQLiteDatabase db; 

    @Override
    public void onCreate(SQLiteDatabase db) {

        String pn_sql = "CREATE TABLE " + PN_TABLE +
                        "(" + PN_KEY_ID + " integer primary key autoincrement, " +
                        PN_KEY_TYPE_OF_SESSION + " text, " +
                        PN_KEY_NAME + " text, " +
                        PN_KEY_START_TIME + " text, " +
                        PN_KEY_END_TIME + " text, " +
                        PN_KEY_ROOM + " text);";
        String wt_sql = "CREATE TABLE " + WT_TABLE +
                        "(" + WT_KEY_ID + " integer primary key autoincrement, " +
                        WT_KEY_TYPE_OF_SESSION + " text, " +
                        WT_KEY_NAME + " text, " +
                        WT_KEY_START_TIME + " text, " +
                        WT_KEY_END_TIME + " text, " +
                        WT_KEY_ROOM + " text);";
        String sr_sql = "CREATE TABLE " + SR_TABLE +
                        "(" + SR_KEY_ID + " integer primary key autoincrement, " +
                        SR_KEY_TYPE_OF_SESSION + " text, " +
                        SR_KEY_NAME + " text, " +
                        SR_KEY_START_TIME + " text, " +
                        SR_KEY_END_TIME + " text, " +
                        SR_KEY_ROOM + " text);";
        String cz_sql = "CREATE TABLE " + CZ_TABLE +
                        "(" + CZ_KEY_ID + " integer primary key autoincrement, " +
                        CZ_KEY_TYPE_OF_SESSION + " text, " +
                        CZ_KEY_NAME + " text, " +
                        CZ_KEY_START_TIME + " text, " +
                        CZ_KEY_END_TIME + " text, " +
                        CZ_KEY_ROOM + " text);";
        String pt_sql = "CREATE TABLE " + PT_TABLE +
                        "(" + PT_KEY_ID + " integer primary key autoincrement, " +
                        PT_KEY_TYPE_OF_SESSION + " text, " +
                        PT_KEY_NAME + " text, " +
                        PT_KEY_START_TIME + " text, " +
                        PT_KEY_END_TIME + " text, " +
                        PT_KEY_ROOM + " text);";

        db.execSQL(pn_sql);
        db.execSQL(wt_sql);
        db.execSQL(sr_sql);
        db.execSQL(cz_sql);
        db.execSQL(pt_sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("Projekt", "aktualizacja z wersji " + oldVersion
              + " do wersji " + newVersion + " ( stare dane ulegna usunieciu ) ");
                 db.execSQL("DROP TABLE IF EXISTS" + PN_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + WT_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + SR_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + CZ_TABLE);
                 db.execSQL("DROP TABLE IF EXISTS" + PT_TABLE);
        onCreate(db);
    }
    public void open() 
    {
            dbhelper.open();
    }
    public void close()
        {
            dbhelper.close();
        }
    public Cursor fetchPn() {
        return db.query(PN_TABLE, new String[] {PN_KEY_TYPE_OF_SESSION, PN_KEY_START_TIME, 
                PN_KEY_END_TIME, PN_KEY_NAME, PN_KEY_ROOM}, null, null, null, null, null);
    }
}

这是我的MainActivity类,它包含 TabHost:

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //--- TABHOST ---//
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, PoniedzialekActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Pn").setIndicator("Pn",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, WtorekActivity.class);
        spec = tabHost.newTabSpec("Wt").setIndicator("Wt",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SrodaActivity.class);
        spec = tabHost.newTabSpec("Sr").setIndicator("Sr",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, CzwartekActivity.class);
        spec = tabHost.newTabSpec("Cz").setIndicator("Cz",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, PiatekActivity.class);
        spec = tabHost.newTabSpec("Pt").setIndicator("Pt",
                          res.getDrawable(R.drawable.ic_launcher))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);
        //--- END OF TABHOST ---//

    }
}
4

1 回答 1

0

setListAdapter()不是为Activity...定义的,所以我不确定你的代码是如何编译的。

您需要扩充您的布局,找到ListViewusing findViewById,然后设置其适配器。

ListView lv = (ListView) findViewById(R.id.pnListView);
lv.setAdapter(...);
于 2012-06-25T21:55:51.877 回答