[英]How do you create an instance variable to call a non-static method from another class?
[英]How to call a non static variable from another class in Android?
請理解,我剛剛開始編碼不到一周,之前我從未制作過應用程序或程序。 我只是在互聯網上查看如何做事並嘗試混合所有內容。 所以我正在制作一個 android 應用程序,我需要從 mysql 數據庫中獲取通知。 我所做的是找到了一個 php 腳本,可以將 mysql 表中的數據轉換為 Json 格式,以便我可以用我的應用程序讀取它。 下面是我顯示數據的部分:Mysql_display.java:
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] licences = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
licences[i] = obj.getString("licence_number");
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, licences);
listView.setAdapter(arrayAdapter);
}
我想要的是在通知上也顯示此信息(許可證):MainActivity.java
public void displayNotification(){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle("Title")
.setContentText("Licences")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
;
NotificationManagerCompat mNotifMgr = NotificationManagerCompat.from(this);
mNotifMgr.notify( 1, mBuilder.build());
}
你就是不能。 為了直接從另一個類訪問變量,變量必須聲明為靜態,否則唯一的方法是創建一個對象並像 myObject.variableName 一樣引用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.