Android:将一个类的实例用于另一个类时遇到问题

时间:2011-02-28 04:48:27

标签: android android-widget

我正在创建sign_up和sign_in页面,并且在editview中输入的数据将存储在数据库中。 所以对于数据库我创建了一个名为DBAdapter.java的类,我的另一个文件是signup.java和login.java。 所以当我点击提交按钮数据时,我想这样做将存储在数据库中。 但问题是我不能将DBAdapter.java的实例数据库用于提交按钮的onclick事件和login.java文件中的登录按钮的相同问题。

提前

谢谢 ......

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_FIRSTNAME = "FirstName";
    public static final String KEY_LASTNAME = "LastName";
    public static final String KEY_USERNAME = "UserName";
    public static final String KEY_PASSWORD = "Password";    
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "master";
    private static final String DATABASE_TABLE = "register";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "FirstName text not null, LastName text not null, " 
        + "UserName text not null, Password text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

和signup.java

public class SignUp extends Activity {

    private EditText firstname;
    private EditText lastname;
    private EditText username;
    private EditText password;

    public String first_name;
    public String last_name;
    public String user_name;
    public String pass_word;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DBAdapter db = new DBAdapter(this);

        firstname = (EditText) findViewById(R.id.ev_fname);
        lastname = (EditText) findViewById(R.id.ev_lname);
        username = (EditText) findViewById(R.id.ev_uname);
        password = (EditText) findViewById(R.id.ev_password);

        final Button button = (Button) findViewById(R.id.btn_submit);
        button.setOnClickListener(new View.OnClickListener() {           
            public void onClick(View v) {              
                first_name = firstname.getText().toString();
                last_name = lastname.getText().toString();
                user_name = username.getText().toString();
                pass_word = password.getText().toString();



                Intent goToNextActivity = new Intent(getApplicationContext(), link.class);
                startActivity(goToNextActivity);


            }
        });

2 个答案:

答案 0 :(得分:0)

我认为这更像是一种黑客攻击,但是什么阻止了你拥有它?

private DBAdapter db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new DBAdapter(this);

我想自己找一个更优雅的解决方案,因为我的应用程序也有多种需求。

答案 1 :(得分:0)

哈利......你试过了吗?

final DBAdapter db = new DBAdapter(this);

而不是:

DBAdapter db = new DBAdapter(this);

JAL