2016-12-14 179 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的應該是公共的和靜態的。

相關問題