简体   繁体   中英

How to call a non static variable from another class in Android?

Please understand that I have just started coding for less than a week now and I have never made an app or a program before. I just look on the internet on how to do things and try to mix everything. So I am making an android app, and I need to get the notification from a mysql database. What I did is that I found a php script that converts the data in the mysql tables to a Json format so that I can read it with my app. Below is the part where I display the data: 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);
    }

What I want is to display this information (licences) on the notification also: 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());

    }

You just can't. In order to access directly a variable from another class, a variable must be declared static, otherwise the only way is creating an oject and reference it like myObject.variableName.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM