如何将ListActivity转换为ListFragment?

时间:2014-12-19 17:49:26

标签: android sqlite android-listfragment listactivity

我有一个数据库,我在Sqlite数据库浏览器中创建并使用库来使用该数据库填充ListActivity中的列表。现在我要将ListActivity转换为ListFragment ...

这是代码......

DataBaseHelper

的代码
    import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

    public class DataBaseHelper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "dictionary.db";
    private static final int DATABASE_VERSION = 1;

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public Cursor getDictionary() {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String [] sqlSelect = {"0 _id", "dream_title", "dream_meaning"}; 
        String sqlTables = "dictionary";

        qb.setTables(sqlTables);
        Cursor c = qb.query(db, sqlSelect, null, null,
                null, null, null);

        c.moveToFirst();
        return c;

    }

}

MainActivity

的代码
public class MainActivity extends ListActivity {

    private Cursor dictionary;
    private DataBaseHelper db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new DataBaseHelper(this);
        dictionary = db.getDictionary(); // you would not typically call this on the main thread

        ListAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                dictionary, 
                new String[] {"dream_title"}, 
                new int[] {android.R.id.text1});

        getListView().setAdapter(adapter);
    }

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

}

任何帮助都会很明显

1 个答案:

答案 0 :(得分:0)

ListFragment使用setListAdapter。我认为这就是你需要改变的全部。