2016-12-14 298 views
0

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

我需要使用这个方法内的数组列表从一个名为数据库帮助器的类中将一个变量的值与另一个类(主菜单)中的这个数组列表进行比较。

的想法是,它会在数据库内的所有值和 检查是否从类主菜单中值的变量是类数据库辅助的数组列表内的任何地方。

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

回答

0

您可以通过调用它从类像实例对象:

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

或改变方法静态,如:

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

然后你只需要调用它:

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

我不完全确定我理解你的问题。我认为phoneArrayList是返回值,你不能保存返回值并使用它吗?这将有助于至少包括完整的方法,最好是全班。

0

enter image description here

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