1

我有一个简单的数据库,其中包含三星型号和价格。

public class DBHelper extends SQLiteOpenHelper implements BaseColumns {

public static final String TABLE_NAME = "samsung";
public static final String NAME = "model";
public static final String MODEL_PRICE = "price";

public DBHelper(Context context) {
    super(context, PhoneProvider.DB_CONTACTS, null, 6);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME
            + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
            + " TEXT, " + MODEL_PRICE + " TEXT);");

    ContentValues values = new ContentValues();

    values.put(NAME, "Galaxy S4 Active");
    values.put(MODEL_PRICE, "680");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy S4");
    values.put(MODEL_PRICE, "659");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy Note II");
    values.put(MODEL_PRICE, "627");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy Mega 6.3");
    values.put(MODEL_PRICE, "581");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy S4 Zoom");
    values.put(MODEL_PRICE, "575");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy SIII");
    values.put(MODEL_PRICE, "523");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy S4 Mini Duos");
    values.put(MODEL_PRICE, "500");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy Mega 5.8");
    values.put(MODEL_PRICE, "418");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy Grand Duos");
    values.put(MODEL_PRICE, "366");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy SII Plus");
    values.put(MODEL_PRICE, "349");
    db.insert(TABLE_NAME, NAME, values);

    values.put(NAME, "Galaxy SIII Mini");
    values.put(MODEL_PRICE, "314");
    db.insert(TABLE_NAME, NAME, values);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
} }

数据库使用 ContentProvider

public class PhoneProvider extends ContentProvider {

public static final String DB_CONTACTS = "phones.db";

public static final Uri CONTENT_URI = Uri.parse(
        "content://com.example.newtransaction.phoneprovider/phone");
public static final int URI_CODE = 1;
public static final int URI_CODE_ID = 2;

private static final UriMatcher mUriMatcher;
private static HashMap<String, String> mContactMap;

private SQLiteDatabase db;

static {
    mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    mUriMatcher.addURI("com.example.newtransaction.phoneprovider", 
            DBHelper.TABLE_NAME, URI_CODE);
    mUriMatcher.addURI("com.example.newtransaction.phoneprovider", 
            DBHelper.TABLE_NAME + "/#", URI_CODE_ID);

    mContactMap = new HashMap<String, String>();
    mContactMap.put(DBHelper._ID, DBHelper._ID);
    mContactMap.put(DBHelper.NAME, DBHelper.NAME);
    mContactMap.put(DBHelper.MODEL_PRICE, DBHelper.MODEL_PRICE);
}

public String getDbName() {
    return(DB_CONTACTS);
}

@Override
public boolean onCreate() {

    db = (new DBHelper(getContext())).getWritableDatabase();
    return (db == null) ? false : true;
}

@Override
public Cursor query(Uri url, String[] projection, 
        String selection, String[] selectionArgs, String sort) {

    String orderBy;       
    if (TextUtils.isEmpty(sort)) {
        orderBy = DBHelper.NAME;
    } 
    else {
        orderBy = sort;
    }

    Cursor c = db.query(DBHelper.TABLE_NAME, projection, selection, selectionArgs, 
            null, null, orderBy);
    c.setNotificationUri(getContext().getContentResolver(), url);
    return c;
}

@Override
public Uri insert(Uri url, ContentValues inValues) {

    ContentValues values = new ContentValues(inValues);

    long rowId = db.insert(DBHelper.TABLE_NAME, DBHelper.NAME, values);
    if (rowId > 0) {
        Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
        getContext().getContentResolver().notifyChange(uri, null);
        return uri;
    }
    else {
        throw new SQLException("Failed to insert row into " + url);
    }
}

@Override
public int delete(Uri url, String where, String[] whereArgs) {
    int retVal = db.delete(DBHelper.TABLE_NAME, where, whereArgs);

    getContext().getContentResolver().notifyChange(url, null);
    return retVal;
}

@Override
public int update(Uri url, ContentValues values, 
        String where, String[] whereArgs) {
    int retVal = db.update(DBHelper.TABLE_NAME, values, where, whereArgs);

    getContext().getContentResolver().notifyChange(url, null);
    return retVal;
}

@Override
public String getType(Uri uri) {       
    return null;
}  }

我需要在片段上添加这个数据库,通过按 RadioButton 显示

public class MainActivity extends FragmentActivity {

FragmentTransaction ft;
Fragment1 frg1;
Fragment2 frg2;
Fragment3 frg3;
Fragment4 frg4;
Fragment5 frg5;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // dbhelper = new DBHelper(this);

    frg1 = new Fragment1();
    frg2 = new Fragment2();
    frg3 = new Fragment3();
    frg4 = new Fragment4();
    frg5 = new Fragment5();


    RadioButton btn1 = (RadioButton) findViewById(R.id.radio1);
    btn1.setChecked(true);
    getSupportFragmentManager().beginTransaction().add(R.id.frame, frg1)
            .commit();

    // set listener
    ((RadioGroup) findViewById(R.id.radio_group))
            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    ft = getSupportFragmentManager().beginTransaction();
                    switch (checkedId) {
                    case R.id.radio1:
                        ft.replace(R.id.frame, frg1);
                        break;
                    case R.id.radio2:
                        ft.replace(R.id.frame, frg2);
                        break;
                    case R.id.radio3:
                        ft.replace(R.id.frame, frg3);
                        break;
                    case R.id.radio4:
                        ft.replace(R.id.frame, frg4);
                        break;
                    case R.id.radio5:
                        ft.replace(R.id.frame, frg5);
                        break;
                    }
                    ft.commit();
                }
            });
}}

片段之一的代码

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    container.removeAllViews();
    return inflater.inflate(R.layout.fragment2, null);
} }

它基于 Goloshchapov - Google Android 一书中的一个示例。移动编程 我是 Android 新手。

4

2 回答 2

0

您需要在片段中添加此代码

DBHelper dbHelper = new DBHelper(getActivity());

于 2014-03-24T09:40:25.600 回答
0

在你的片段中

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

      PhoneProvider pp =new PhoneProvider();

      pp.query(your query);


    return inflater.inflate(R.layout.fragment2, null);
} }
于 2013-09-09T12:56:14.253 回答