从另一个类调用arraylist方法

时间:2016-12-14 01:10:55

标签: java android database

如何在另一个类中调用此方法?

我需要在一个名为database helper的类中使用此方法中的数组列表,以将变量的值与另一个类(Main Menu)中的此数组列表进行比较。

这个想法是它获取数据库中的所有值 检查类Main Menu值中的变量是否在类数据库助手的数组列表中的任何位置。

  public ArrayList<Phone> retrieveAllPhones() {
        ArrayList<Phone> phoneArrayList
                = new ArrayList<>();
// SELECT * FROM student;
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(Phone.TABLE,
                null,// columns
                null,// selection
                null,// selection args
                null,// group by
                null,// having
                null// order by
        );

        if (cursor.moveToFirst()) {
            do {
                Phone phone = new Phone();

                phone.setSenderNum(
                        cursor.getString(cursor.getColumnIndex(Phone.NUM))
                );
                // assign this name to Student

                phone.setMessage(
                        cursor.getString(cursor.getColumnIndex(Phone.MESSAGE))

                );
                phone.setGeo(
                        cursor.getString(cursor.getColumnIndex(Phone.GEO))
                );

                phoneArrayList.add(phone);
            } while (cursor.moveToNext());
        }

3 个答案:

答案 0 :(得分:0)

我不完全确定我理解你的问题。我假设phoneArrayList是返回值,你不能只保存返回值并使用它吗?至少包括完整的方法,最好包括完整的方法。

答案 1 :(得分:0)

你可以通过类中的实例化对象来调用它,如:

ArrayList<Phone> phones = new YourDatabaseHelperClass().retrieveAllPhones();

或将方法更改为静态,如:

public static ArrayList<Phone> retrieveAllPhones() {
  ...
}

然后你只需要用:

来调用它
ArrayList<Phone> phones = YourDatabaseHelperClass.retrieveAllPhones();

答案 2 :(得分:0)

enter image description here

可以从另一个Class访问ArrayList,但ArrayList应该是公共的和静态的。