开发者

Method Calls (static to non-static) for SQL and ListActivity

I've read a ton of posts across the net and see many others want to do the same thing and all posts refer to needing to do something like a Singleton or use cumbersome conext routines. Why is accessing a Database wrapper class more difficult than accessing any other activity (which is pretty simple)? I have no problem with accessing other activities from another activity - I must have been napping during that class - what am I missing?

Here's the background:

Maincode calls up a main layout with One Button.

The Button calls a DB Wapper class (called DB_Interface, which has a layout with buttons to do the DB things I want. The main DB stuff is defined in the helper - I'm calling it DBHelp_mate).

Without using the Maincode, I can run the app (using the DB_Interface as the primary activity) and it works just fine. But, as soon as I use the Maincode to call DB_Interface, it crashes when pressing the button.

Yes - I have properly changed the Manifest file to reflect which Activity is the main one as well as declaring the other activities (it's not rocket science - and, truly, I am a rocket scientist!).

So, my question is this: how can I call DB_Interface from Maincode and have it call up the DBHelp_mate and work?

Thanks - the new codes are a follows (sans the stuff inside the DBHelp_mate that shouldn't affect your guidance). Please provide specific calls/code rather than saying things like "you need to instantiate. this and that" - that just makes for more confusion and many replies/re-posts.

NEW CODE:

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

    final Button goToDB = (Button) findViewById(R.id.Button01);
    //  ---------------------------------------------------------
    goToDB.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        Intent doIt = new Intent();
        doIt.setClassName("com.bt.hopex", "com.bt.hopex.DB_Interface");
        startActivity(doIt);
      }
    }); // end -----------------------------------------------   
  }//end onCreate 
}//end activity

DB_INTERFACE

  public class DB_Interface extends Activity { 
  private DBHelp_mate ddbb;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.db_dialog);       
        ddbb = new DBHelp_mate(this);
        ddbb.open();       
    }//end onCreate
}//end activity 

DBHelp-mate

public class DBHelp_mate {

  private DatabaseHelper dbHelper ;
  private SQLiteDatabase db;

  private Context mCtx = null;

  //*******************************************************
  private static final String DATABASE_NAME = "XgAlert_db";
  private static final String DATABASE_TABLE = "tblData";
  private static final int    DATABASE_VERSION = 2;
  private static final String DATABASE_CREATE =
      "create table gAlert_alerts ( "
      + "_id integer primary key autoincrement, "
      + "alert_text text not null,"
      + "alert_date varchar(20) not null,"
      + "alert_time varchar(20) not null"
      +");";

   //-------------------------------------------------------------
  private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context ctx) {
      super(ctx, 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){
      db.execSQL("DROP IF TABLE EXISTS "+ DATABASE_TABLE); change drop to alter later after all is working!
      onCreate(db);
    }
  }

  public DBHelp_mate(Context ctx) {
    mCtx = ctx;
  }
  //********************************************************

  public DBHelp_mate open () throws SQLException {
    dbHelper = new DatabaseHelper(mCtx);
    db = dbHelper.getWritableDatabase();
    return this;
  }
  //------------------------------------------------------------
  /** Closes a database connection */
  public void close() {
    dbHelper.close();
  }    
  //-----------开发者_StackOverflow中文版-------------------------------------------------


Given I can't change the non-static method to static, how can I call this method from another activity?

You don't. You refactor your code such that your data model is accessible from all activities that need it.


The problem is that you have to create an instance of the class OptPanel, like

OptPanel myOptPanel = new OptPanel();

Only in that way you are able to access the method showRecords() and give the myOptPanel as parameter to another activity (like the constructor).

public class MyActivity
{
    private OptPanel _optPanel;

    public MyActivity(OptPanel myOptPanel)
    {
        this._optPanel = myOptPanel;
    }

    public void showRecordsInActivity()
    {
        this._optPanel.showRecords(null);
    }
}

If you write

MyClass.myStaticClassMethod();

you can only access static members.

If you write

MyClass theClass = new MyClass();
theClass.MyObjectMethod();

you can access object members.

This is common OOP syntax.


SOLVED

After many days of struggling with this, I bought a book - Android for Dummies and, Bingo, the how to do it buried in the book. For those interested in how to do it, read the relevant sections of the book.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜