如何使用另一个类SQLite数据库管理类get方法? [英] How to use a get method for a SQLite Database management class in another class?

查看:145
本文介绍了如何使用另一个类SQLite数据库管理类get方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是从另一台数据库管理器类的ArrayList。不幸的是我所能做的,因为经理类不能静态地工作,是在另一个类中创建一个实例,然后调用该方法。然后,我开始学着传递一个相同的实例在其中要求一个 SQLiteDatabase 对象的方法。现在,我把自己累得混乱的困境,当我真正想要的是做的是检索数组列表从一个SQL列中显示的元素列表视图。

编辑:我的职位缺乏明确性,所以我会尽力来指定到底是怎么回事错的,什么我想在这里完成的:

在一个显示(输出)的活动,我想用一个ListView显示包含在SQL数据库元素。目前,我只专注于一列(分配名称)。我的方法包括利用内置于数据库管理器类 GET 方法,而是因为你不能引用方法静态,我试图通过创建管理器类的实例使用的方法。这将返回输入对象的ArrayList(每片含名称)。这似乎已经奏效,但在运行程序时,在LogCat中抗议说,我是递归调用getDatabase。网上通缉后,人们建议我改变方法,以请求(SQLiteDatabase DB)作为参数,这样相同的数据库获取经理到处滥用解决问题。现在我弄糊涂这里 - 我不知道该怎么传递到从显示的活动这种方法。它也没有帮助,从我从听到的意见,我的 GET 方法不正确遍历SQL数据库。如果你能解决这个难题THANK YOU!

我会后我的code诊断,希望外界认为将呈现什么是错的一切我想在这里。

 公共光标getAssignmentNames(SQLiteDatabase DB){
    返回db.query(假,ASSIGNMENT_TABLE,COLUMN_TITLES,
            WHE​​RE+ ASSIGNMENT_NAME +,NULL,NULL,NULL,ORDER BY+ ASSIGNMENT_URGENCY_RATING,NULL);
}

/

 公开的ArrayList<输入> getListOfAssignments(SQLiteDatabase DB){
        游标名= getAssignmentNames(DB);
        ArrayList的<输入> assList =新的ArrayList<输入>();
        names.moveToFirst();
        而(!cursorsAreAfterLast(地名)){
            INT去= 0;
            assList.add(新输入(names.getString(GO))            names.moveToNext();
            去++;
        }
        返回assList;
    }

/

  DBRecordsLayer assignmentRecords =新DBRecordsLayer(这一点,
                assignment.db,NULL,1);
        ArrayList的<输入> assList = DBRecordsLayer.getListOfAssignments(assignmentRecords);


解决方案

您code是一个有点混乱......在while循环的每次迭代,你递增光标(names.moveToNext());您还递增去了。

其结果将是:

第1次迭代:你从第一查询的第一列中取数据

第二次迭代:你是从第二个查询的第二列取数据

等等......

我假设你想从数据库中每次迭代的同一列中读取数据。

试试这个:

 公开的ArrayList<输入> getListOfAssignments(SQLiteDatabase DB){
    游标名= getAssignmentNames(DB);
    ArrayList的<输入> assList =新的ArrayList<输入>();    names.moveToFirst();
    columnContainingStringToSendToInputtedConstructor = X; //替换列从您的表需要的X
    而(!names.isAfterLast()){
        assList.add(新输入(names.getString(columnContainingStringToSendToInputtedConstructor));        names.moveToNext();
    }
}

What I am trying to do is retrieve an ArrayList from another database manager class. Unfortunately all I can do because the manager class cannot work statically is create an instance in another class, then call the method. Then I got myself into passing that same instance into the method which asked for an SQLiteDatabase object. Now I've worked myself into a bind of confusion, when all I really want is to do is retrieve the arraylist to display a listview of elements from an SQL column.

EDIT: My post lacked clarity, so I'll try to specify exactly what is going wrong and what I am trying to accomplish here:

In a display (output) activity, I am trying to use a ListView to display elements contained in an SQL database. Currently, I am only focusing on one column (Assignment Names). My approach involved using a get method built into the database manager class, but because you cannot reference that method statically, I tried to use the method by creating an instance of that manager class. This would return an ArrayList of Inputted objects (each containing a name). It seemed to have worked, but when running the program, the LogCat protested that I was calling getDatabase recursively. After looking online, people recommended that I fix the issue by changing the method to ask for (SQLiteDatabase db) as parameters so the same database gets tossed around in the manager. Now I get confused here-- I'm not sure what to pass into this method from the display activity. It also doesn't help that from what I've heard from the comments, my get method doesn't traverse the SQL database properly. If you can solve this puzzle THANK YOU!

I'll post my code for diagnosis, hopefully an outside view will show exactly what's wrong with everything I'm trying here.

public Cursor getAssignmentNames(SQLiteDatabase db) {
    return db.query(false, ASSIGNMENT_TABLE, COLUMN_TITLES, 
            " WHERE " + ASSIGNMENT_NAME + " ", null, null, null, " ORDER BY "+ASSIGNMENT_URGENCY_RATING, null);
}

/

    public ArrayList<Inputted> getListOfAssignments (SQLiteDatabase db) {
        Cursor names = getAssignmentNames(db);
        ArrayList<Inputted> assList = new ArrayList<Inputted>();
        names.moveToFirst();
        while (!cursorsAreAfterLast(names) ) {
            int go = 0;
            assList.add(new Inputted(names.getString(go))

            names.moveToNext();
            go++;
        }
        return assList;
    }

/

DBRecordsLayer assignmentRecords = new DBRecordsLayer(this,
                "assignment.db", null, 1);


        ArrayList<Inputted> assList = DBRecordsLayer.getListOfAssignments(assignmentRecords);

解决方案

Your code is a bit confusing... In each iteration of the while loop, you are incrementing the cursor (names.moveToNext()); You are also incrementing go.

The result would be:

1st iteration: You are taking the data from the first column of the first query

2nd iteration: You are taking the data from the second column of the second query

etc...

I'm assume that you want to be reading data from the same column of the database for each iteration.

try this:

public ArrayList<Inputted> getListOfAssignments (SQLiteDatabase db) {
    Cursor names = getAssignmentNames(db);
    ArrayList<Inputted> assList = new ArrayList<Inputted>();

    names.moveToFirst();
    columnContainingStringToSendToInputtedConstructor = x;  //replace the x with column you need from your table
    while (!names.isAfterLast()) {
        assList.add(new Inputted(names.getString(columnContainingStringToSendToInputtedConstructor));

        names.moveToNext();
    }
}

这篇关于如何使用另一个类SQLite数据库管理类get方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆