简体   繁体   English

无法从其他类调用函数

[英]Unable to call a function from an other class

I'm creating an app on Android studio using java and a SQLite database. 我正在使用Java和SQLite数据库在Android Studio上创建一个应用程序。 I have created a function to get data from the database in a list but i am not able to call it from an other class. 我已经创建了一个函数来从列表中的数据库中获取数据,但无法从其他类中调用它。

public List getNumTicket(){
    List NumTicket=new ArrayList();
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT nbTicket FROM TicketCaisse";
    Cursor cursor = db.rawQuery(query, null);
    while(cursor.moveToNext()){
        NumTicket.add(cursor.getInt(0));
    }
    db.close();
    return NumTicket;
}

This function is in a class which is using SQLiteOpenHelper 该函数在使用SQLiteOpenHelper的类中

public class BaseSQLite extends SQLiteOpenHelper{

public BaseSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
    super(context,name,factory,version);
}

And i'm trying to call it from the class "accueil" 我想从类“ accueil”中称呼它

public class Accueil extends AppCompatActivity {

I would be very happy if you could help me on this one . 如果您能在这方面帮助我,我将非常高兴。 If you need more infos ask me ;) 如果您需要更多信息,请问我;)

public class Accueil extends AppCompatActivity {
oncreate{
BaseSQLite bsqlite = new BaseSQLite(//pass constructors what you are need);
bsqlite.getNumTicket();

The function belongs to the BaseSQLite class thus you need to instiantiate (create) a BaseSQlite object which uses the class constructor. 该函数属于BaseSQLite类,因此您需要实例化(创建)一个使用类构造函数的BaseSQlite对象。

The class constructor is the following :- 类的构造函数如下:

public BaseSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
    super(context,name,factory,version);
}

So you need to have code such as 所以你需要像这样的代码

    BaseSQLite name_of the_object = new BaseSQLite(
        context_to_be_used,
        Database_name,
        cursor_factory, // null will very likely suit
        version
    );

Once you have the object ( name_of_the object if the above were used) you can then use it's methods (functions) so name_of_object.getNumTicket() will invoke the method(function). 一旦有了对象(如果使用了name_of_the object ,则使用了该对象),便可以使用它的方法(函数),因此name_of_object.getNumTicket()将调用method(function)。

More specifically :- 进一步来说 :-


In Accueil 's onCreate method after the setContentView line have :- AccueilonCreate方法中,在setContentView行之后有:-

  BaseSQLite mybasesqlite = new BaseSQLite(this,"your_database_name",null,1); List mylist = mybasesqlite.getNumTicket(); 

Notes 笔记

  • typically you'd have the database name null and version hard coded in the constrcutor, so you'd then only need to use BaseSQLite mybasesqlite = new BaseSQLite(this); 通常,您将在BaseSQLite mybasesqlite = new BaseSQLite(this);使用数据库名称null和版本硬编码,因此您只需要使用BaseSQLite mybasesqlite = new BaseSQLite(this);

  • eg The If the database were mydatabase.d b and the version were 1 then you could have :- 例如,如果数据库为mydatabase.d b,版本为1,则可能有:-

    public BaseSQLite(Context context) { super(context,"mydatabase.db",null,1); }

  • null for the cursor factory is very likely fine at this stage. 在此阶段,游标工厂的null可能很好。


However, I'd suggest having a single occurrence (constant), of the database name, version table names and columns and always refer to that eg 但是,我建议对数据库名称,版本表名称和列进行一次(常量)查找,并始终引用例如

public class BaseSQLite extends SQLiteOpenHelper{

    public static final String DBNAME = "mydatabase.db";
    public static final int DBVERSION = 1;
    public static final String TB_TICKETCAISSE = "TicketCaisse";
    public static final String COL_TICKETCAISSE_NBTICKET = "nbTicket"

    public BaseSQLite(Context context) {
        super(context,DBNAME,null,DBVERSION);
    }
    ......
    public List getNumTicket(){
        List NumTicket=new ArrayList();
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT " + COL_TICKETCAISSE_NBTICKET + " FROM " + TB_TICKETCAISSE;
        Cursor cursor = db.rawQuery(query, null);
        while(cursor.moveToNext()){
            NumTicket.add(cursor.getInt(csr.getColumnIndex(COL_TICKETCAISSE_NBTICKET)));
        }
        cursor.close() //<<<< ADDED should always close Cursors when done with them
        db.close();
        return NumTicket;
}

The corresponding Activity could be along the lines of :- 相应的活动可能类似于:

public class Accueil extends AppCompatActivity {

    BaseSQLite  mybasesqlite;      

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mybasesqlite = new BaseSQLite(this);
        List mylist = mybasesqlite.getNumTicket();

    }
    // if used in another method then
    private void doSomethingElse() {
        List myotherlist = mybasesqlite.getNumTicket();
    }
}

Create BaseSQLite.class object 创建BaseSQLite.class对象

public List getNumTicket(){
    List NumTicket=new ArrayList();
    BaseSQLite db = this.getWritableDatabase();
    String query = "SELECT nbTicket FROM TicketCaisse";
    Cursor cursor = db.rawQuery(query, null);
    while(cursor.moveToNext())
      {
      NumTicket.add(cursor.getInt(0));
      }
    db.close();
    return NumTicket;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM