2

我对 Fragments 很陌生,并试图学习如何使用它们。我是出于爱好而编程,所以请不要太苛刻... ;)

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

我的问题是,如何正确连接 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>

如果您需要 Adapter 或/和 Handler 的代码,请告诉我。谢谢你的帮助。

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

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);
    }

}

当我在编辑这篇文章时,我想到了我可能有一个导入错误,但我无法找出其中一个是否错误,因为这对我来说也是新的。我希望我没有简单地忘记一些事情,那会很尴尬......

4

2 回答 2

1

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

于 2013-05-02T21:56:00.770 回答
0

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

于 2013-04-29T20:47:09.080 回答