无法从其他类调用函数

时间:2018-01-29 08:15:36

标签: java android sqlite

我正在使用java和SQLite数据库在Android studio上创建应用。 我创建了一个函数来从列表中的数据库中获取数据,但我无法从其他类中调用它。

#include<iostream>
#include<algorithm>
using namespace std;

int split_count(string str,char delimit){
return count(str.begin(),str.end(),delimit);
}

void split(string str,char delimit,string res[]){
int a=0,i=0;
while(a<str.size()){
res[i]=str.substr(a,str.find(delimit));
a+=res[i].size()+1;
i++;
}
}

int main(){

string a="abc.xyz.mno.def";
int x=split_count(a,'.')+1;
string res[x];
split(a,'.',res);

for(int i=0;i<x;i++)
cout<<res[i]<<endl;
  return 0;
}

此函数位于使用SQLiteOpenHelper

的类中
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;
}

我试图从课堂上拨打电话&#34; accueil&#34;

public class BaseSQLite extends SQLiteOpenHelper{

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

如果你能帮助我,我将非常高兴。如果您需要更多信息,请问我;)

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

该函数属于BaseSQLite类,因此您需要实现(创建)使用类构造函数的BaseSQlite对象。

类构造函数如下: -

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

所以你需要拥有像

这样的代码
    BaseSQLite name_of the_object = new BaseSQLite(
        context_to_be_used,
        Database_name,
        cursor_factory, // null will very likely suit
        version
    );

一旦你拥有了对象(如果使用了上面的name_of_the object),你可以使用它的方法(函数),这样name_of_object.getNumTicket()将调用方法(函数)。

更具体地说: -

Accueil行之后的onCreate setContentView方法中: -

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

备注

  • 通常你在构造函数中有数据库名称null和版本硬编码,所以你只需要使用BaseSQLite mybasesqlite = new BaseSQLite(this);

  • e.g。如果数据库是 mydatabase.d b且版本 1 那么您可以: -

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

  • 光标工厂的
  • null在这个阶段非常可能。

但是,我建议只有一次出现(常量)数据库名称,版本表名称和列,并且总是引用它。例如。

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;
}

相应的活动可以是: -

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();
    }
}

答案 2 :(得分:0)

创建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;
}