如何正确连接SQLite数据库与ListFragment?

时间:2013-04-28 18:34:15

标签: android android-sqlite android-listfragment

我对Fragments很新,并试图学习如何使用它们。我为爱好做编程,所以请不要苛刻......;)

所以我使用Eclipse助手设置了一个带有可滑动标签的应用程序。此时,两个选项卡与正常的片段连接,这是有效的。但是我想要一个显示数据库内容的第三个选项卡。

我的问题是,如何正确连接ListFragment和数据库?我得到的只是一个不间断运行的ProgressCircle。

我已经掌握了ListActivitys和SQLiteDatabases的一些经验,我自己定位了这些经验。所以我想我的数据库代码没问题,因为我没有错误。我相信我的错误是在ListFragment类中,但我不知道该做什么,因为Eclipse没有显示任何错误...当Android 3正在进行时我也使用数据库,所以我不想排除那些东西可能已经改变了......

所以这是我的ListFragment代码:

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GefilterteListe extends ListFragment {

    public static final String TAG = GefilterteListe.class.getSimpleName();

    private FilterDatenbankHandler dbHandler;
    private Cursor dbCursor;
    private LehrerDatenbankAdapter dbAdapter;
    private Context mContext;

    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        Log.i(TAG, "GefilterteListe onCreate");
        View returnView = inflater.inflate(R.layout.plan, container, false);
        mContext = getActivity();
        dbHandler = new FilterDatenbankHandler(mContext);
        dbCursor = dbHandler.query();
        ((Activity) mContext).startManagingCursor(dbCursor);
        dbAdapter = new LehrerDatenbankAdapter(mContext, dbCursor);
        setListAdapter(dbAdapter);

        return returnView;
    }

}

这是我想要显示数据的plan.xml:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Krzl"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="36sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Tag"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Datum"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/textView2"
        android:text="Stunde: "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toRightOf="@+id/textView4"
        android:text="Stunde"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="Merkmal: "
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView6"
        android:layout_alignBottom="@+id/textView6"
        android:layout_toRightOf="@+id/textView1"
        android:text="Merkmal"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView6"
        android:text="Info: "
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignBottom="@+id/textView8"
        android:layout_toRightOf="@+id/textView8"
        android:text="Info"
        android:textAppearance="?android:attr/textAppearanceSmall" />


</RelativeLayout>

如果您需要适配器或/和处理程序的代码,请告诉我。谢谢你的帮助。

修改 在这句话下面我添加了处理程序和适配器。 处理程序:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FilterDatenbankHandler extends SQLiteOpenHelper{

    private static final String TAG = FilterDatenbankHandler.class.getSimpleName();

    //Name und Version der Datenbank
    private static final String DATABASE_NAME = "Lehrer.db";
    private static final int DATABASE_VERSION = 1;

    //Name und Attribute der Tabelle "vertretungsplan"
    private static final String _ID = "_id";
    private static final String TABLE_NAME = "Lehrerdatenbank";
    private static final String KUERZEL = "kürzel";
    private static final String WOCHENTAG = "tag";
    private static final String DATUM = "datum";
    private static final String STUNDE = "stunde";
    private static final String MERKMAL = "merkmal";
    private static final String INFO = "info";

    //Tabelle "vertretungsplan11" anlegen
    private static final String TABLE_CREATE = " CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KUERZEL + " STRING, " + WOCHENTAG + " STRING, " + DATUM + " STRING, " + STUNDE + " STUNDE, " + MERKMAL
            + " STRING, " + INFO + " STRING ); ";

    //Tabelle "test" löschen
    private static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME;

    FilterDatenbankHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
//      Log.i(TAG, "DatenbankHandler wurde initiiert.");
    }   


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(TABLE_CREATE);
        Log.i(TAG, "FilterDatenbankHandler: " + db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgraden der Datenbank von Version " + oldVersion + " zu Version " + newVersion + "; alle Daten werden gelöscht");
        db.execSQL(TABLE_DROP);
        onCreate(db);
    }

    public void redoTbl(){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL(TABLE_DROP);
        Log.i(TAG, "Lehrer-Tabelle gelöscht");
        onCreate(db);
    }

    public void insert(String kuerzel, String tag, String datum, String stunde, String merkmal, String info){
        long rowId = -1;
        try {
            SQLiteDatabase db = getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KUERZEL, kuerzel);
            values.put(WOCHENTAG, tag);
            values.put(DATUM, datum);
            values.put(STUNDE, stunde);
            values.put(MERKMAL, merkmal);
            values.put(INFO, info);
            rowId = db.insert(TABLE_NAME, null, values);
            Log.i(TAG, "insert: " + kuerzel + ", " + tag + ", " + datum + ", " + stunde + ", " + merkmal + ", " + info); //I get this Log, so i suppose it does work.
        } catch (SQLiteException e) {
            Log.i(TAG, "insert()", e);
        } finally {
            Log.i(TAG, "insert(): rowId=" + rowId);
        }
    }

    public Cursor query() {
        SQLiteDatabase db = getWritableDatabase();
        return db.query(TABLE_NAME, null, null, null,
                null, null, _ID +  " ASC" );
    }

}

适配器:

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class LehrerDatenbankAdapter extends CursorAdapter{

    private LayoutInflater inflator;
    private int s;
    String TAG = "LehrerDatenbankAdapter";

    public LehrerDatenbankAdapter(Context context, Cursor c){
        super(context, c);
        inflator = LayoutInflater.from(context);
        s = c.getCount();
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
        TextView textview1 = (TextView) view.findViewById(R.id.textView1);//Kürzel
        TextView textview2 = (TextView) view.findViewById(R.id.textView2);//tag
        TextView textview3 = (TextView) view.findViewById(R.id.textView3);//datum
        TextView textview4 = (TextView) view.findViewById(R.id.textView5);//Stunde
        TextView textview5 = (TextView) view.findViewById(R.id.textView7);//Merkmal
        TextView textview6 = (TextView) view.findViewById(R.id.textView9);//Info

        textview1.setText(cursor.getString(1));
        textview2.setText(cursor.getString(2));
        textview3.setText(cursor.getString(3));
        textview4.setText(cursor.getString(4));
        textview5.setText(cursor.getString(5));
        textview6.setText(cursor.getString(6));

        cursor.moveToNext();
    }

    @Override
    public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        return inflator.inflate(R.layout.plan, null);
    }

}

当我编辑这篇文章的时候,我明白了,我可能有一个导入错误,但我无法找到,如果其中一个是错的,因为这对我来说也是新的。 我希望我不会忘记一些令人尴尬的事情......

2 个答案:

答案 0 :(得分:1)

我明白了!我只需要编写一个onCreate方法,并且必须在那里插入我的代码,而不是在onCreateView方法中...

答案 1 :(得分:0)

提供dbAdapter和dbHandler代码将有所帮助,因为它将显示您是否实际打开以及如何查询数据库。您可能想要查看SimpleCursorAdaptor。另外,如果您使用的是4.x,您可能希望结合使用Cursorloader和LoaderManager,它们提供了打开和关闭数据库以及所有线程内容的所有工作(请查看此http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html),但是我建议您在切换到加载器之前确保您的游标和数据库查询功能齐全并且正常工作。我有一个没有加载器的功能齐全的应用程序,但遇到了listview滚动的一些UI性能问题。切换到装载机解决了这一切。