【问题标题】:How to call a non static variable from another class in Android?如何从 Android 中的另一个类调用非静态变量?
【发布时间】:2019-07-18 12:17:00
【问题描述】:

请理解,我刚刚开始编码不到一周,而且我以前从未制作过应用程序或程序。我只是在互联网上查看如何做事并尝试混合所有内容。 所以我正在制作一个 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());

    }

【问题讨论】:

  • 如果你的编码时间不到一周,你应该从学习基础开始。要么是静态的,可以通过类调用,要么不是静态的,只能通过类的实例调用
  • 我根据我所处的项目阶段了解我必须学习的内容
  • @Amine 我说的是你应该在考虑开始一个项目之前至少一年学到的东西。

标签: java android


【解决方案1】:

你就是做不到。为了直接访问另一个类中的变量,必须将变量声明为静态变量,否则唯一的方法是创建一个对象并像 myObject.variableName 一样引用它。

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多