简体   繁体   English

如何从Android中的另一个类调用非静态变量?

[英]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.所以我正在制作一个 android 应用程序,我需要从 mysql 数据库中获取通知。 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.我所做的是找到了一个 php 脚本,可以将 mysql 表中的数据转换为 Json 格式,以便我可以用我的应用程序读取它。 Below is the part where I display the data: Mysql_display.java:下面是我显示数据的部分: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我想要的是在通知上也显示此信息(许可证):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.为了直接从另一个类访问变量,变量必须声明为静态,否则唯一的方法是创建一个对象并像 myObject.variableName 一样引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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