0

我在我的 Android 项目中创建了一个 SQLite 数据库,以使用 SqliteOpenHelper 存储有关房屋的信息。我有两个活动。一个是包含所有房屋列表的列表活动,另一个是由 listActivity 启动以添加新房屋。我的问题与数据库的使用有关。哪个更好?在这两个活动中都有一个数据库实例?或者仅在一个活动中拥有数据库的静态实例并在需要访问它的所有其他活动中使用它?通过不同的活动使用数据库的更好方法是什么?

public class HomeDatabaseHandler extends SQLiteOpenHelper{

    //Database Version
    private static final int DATABASE_VERSION = 1;

    //Database name static value
    private static final String DATABASE_NAME = "homeManager";

    //Table name
    private static final String HOMES_TABLE = "homes";
    //more code here for adding creating etc...
}


public class MainActivity extends ListActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Here is my db for populating listView
        HomeDatabaseHandler db = new HomeDatabaseHandler(this);

}


//in Activity to add home
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
         case R.id.action_add_done:
             //more code here to get values from views
             //create new db to add values
             HomeDatabaseHandler db = new HomeDatabaseHandler(this);

             //method to add home
             db.addHome

    }
}    

这就是它现在的样子,创建 Handler 的实例并添加。那你怎么看?我觉得这不是最好的方法......

4

2 回答 2

0

好吧,我会说,一旦您的广告发布,就可以正确地进行操作,然后您将来就不必头疼了。
这是一个简单直接的示例,您可以在半天之内启动并运行。

使用 MySql 的 Android 内容提供程序

于 2013-09-11T22:18:23.237 回答
0

方法覆盖是在不同活动中使用 SQLite 的最佳方式。

适配器.java

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

import com.bambeeq.conferencecall.domain.ProfileDo;
import com.bambeeq.conferencecall.domain.StepDO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ConferenceAdapter {
    SQLiteDatabase database_ob;
    ConferenceOpenHelper openHelper_ob;
    Context context;

    public ConferenceAdapter(Context c) {// constructor
        context = c;
    }


    public ConferenceAdapter opnToRead() {// method for open and read the database to perform the operations
        openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getReadableDatabase();
        return this;
    }

    public ConferenceAdapter opnToWrite() {// method for open and write the database to perform the operations
        openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;
    }

    public void Close() {// method for closing the database
        database_ob.close();
    }

    public long insertDetails(String value, String delay, int profileId) {// insert method for steps
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.VALUE, value);
        contentValues.put(openHelper_ob.DELAY, delay);
        contentValues.put(openHelper_ob.PROFILE_ID, profileId);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.STEP_TABLE_NAME, null, contentValues);
        Close();
        return val;
    }


    public long insertProfile(String profile, String status,String optionstatus) { // method to insert the profile name
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.PROFILE, profile);
        contentValues.put(openHelper_ob.STATUS, status);
        contentValues.put(openHelper_ob.OPTION_STATUS, optionstatus);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.PROFILE_TABLE_NAME, null, contentValues);
        Close();
        return val;
    }

    public Cursor queryForSteps(int profileId) {// method to display the edit profile name and its related steps.
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.PROFILE_ID + "=" + profileId, null, null, null, null);
        return c;
    }

    public Cursor queryAll(int stepId) {// method to display the edit profile
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.KEY_ID + "=" + stepId, null, null, null, null);
        return c;
    }
public long updateldetail(int rowId, String value, String delay) {// method to update the value and delay
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.KEY_ID, rowId);
        contentValues.put(openHelper_ob.VALUE, value);
        contentValues.put(openHelper_ob.DELAY, delay);
        opnToWrite();
        long val = database_ob.update(openHelper_ob.STEP_TABLE_NAME, contentValues, openHelper_ob.KEY_ID + "=" + rowId, null);
        Close();
        return val;
    }
public int deleteHomeRecord(int profileId) {// method to delete the profileId
        opnToWrite();
        int vals = database_ob.delete(openHelper_ob.STEP_TABLE_NAME, openHelper_ob.PROFILE_ID + "=" + profileId, null);
        int val = database_ob.delete(openHelper_ob.PROFILE_TABLE_NAME, openHelper_ob.P_ID + "=" + profileId, null);
        Close();
        return val;
    }

OpenHelper.java

public class ConferenceOpenHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "confDB";
    public static final String PROFILE_TABLE_NAME = "profile";
    public static final String STEP_TABLE_NAME = "step";
    public static final int VERSION = 3;    
    public static final String KEY_ID = "_id";
    public static final String P_ID = "_id";
    public static final String TEMP_VARIABLE = "0";
    public static final String  INSERT_TEMPLATE= "0";
    public static final String TEMPLATE = "template";
    public static final String VALUE = "value";
    public static final String DELAY = "delay";
    public static final String PROFILE_ID = "profile_id";
    public static final String PROFILE = "profile";
    public static final String STATUS = "status";
    public static final String OPTION_STATUS = "optionstatus";

//query for creating tables for profile and steps
    private static final String CREATE_PROFILE_TABLE = "create table profile(_id integer not null primary key autoincrement,profile text null,status text not null,optionstatus text not null)";
    private static final String CREATE_STEP_TABLE = "create table step (_id integer not null primary key autoincrement,value text null,delay integer null,profile_id integer default 0, FOREIGN KEY (profile_id) REFERENCES profile(id))";

    //constructor
    public ConferenceOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(CREATE_PROFILE_TABLE);
        db.execSQL(CREATE_STEP_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        onCreate(db);
    }

}

插入方法:

@覆盖

public void onClick(DialogInterface dialog, int whichButton) {

                    String profile = profileEditText.getText().toString();


                    long vals = adapter.insertProfile(profile, HOME,CUSTOM_DIALER);

您可以覆盖这样的方法进行更新,删除。

于 2013-09-12T10:00:14.423 回答