Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从json对象改装中的布尔值_Java_Android_Android Studio_Retrofit - Fatal编程技术网

Java 从json对象改装中的布尔值

Java 从json对象改装中的布尔值,java,android,android-studio,retrofit,Java,Android,Android Studio,Retrofit,我的json对象中有一个设置为布尔值的状态。我的主要问题是,在每次处理的时间里,比如说5秒,它将添加另一组状态,这是之前显示的状态,并且没有结束。我不确定是什么触发了这一点,有人能帮我吗?我刚刚开始学习android。从onViewCreated调用getSystemObject,而不是从onResume调用 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(

我的json对象中有一个设置为布尔值的状态。我的主要问题是,在每次处理的时间里,比如说5秒,它将添加另一组状态,这是之前显示的状态,并且没有结束。我不确定是什么触发了这一点,有人能帮我吗?我刚刚开始学习android。

onViewCreated
调用
getSystemObject
,而不是从
onResume
调用

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    handler.postDelayed(runnable = new Runnable() {
                public void run() {
                    //do your function;
                    getSystemObject();
                    handler.postDelayed(runnable, apiDelayed);
                }
            }, apiDelayed);
}
更新2 在添加任何新内容之前,需要清除TextView的旧内容

//hardware
hardwareStatus.setText("") // Clear old response data from TextView
for (int i = 0; i < response.body().getHardware().length; i++){
  // Your logic
}

//software
softwareStatus.setText("") // Clear old response data from TextView
for (int i = 0; i < response.body().getSoftware().length; i++){
  // Your logic
}
只用

hardwareStatus.setText(spannable);
古老的 据我所知,您正在运行一个每5秒触发一次的无限api调用循环,如果是这种情况,那是因为您正在runnable内部再次调用postDelayed函数,如果希望只调用一次端点,请删除这一行

handler.postDelayed(runnable = new Runnable() {
            public void run() {
                //do your function;
                getSystemObject();
                handler.postDelayed(runnable, apiDelayed); // Remove this
            }
        }, apiDelayed);
这里需要注意的另一件事是,您应该在

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        hardwareName = view.findViewById(R.id.hardware_name);
        hardwareStatus = view.findViewById(R.id.hardware_status);

        softwareName = view.findViewById(R.id.software_name);
        softwareStatus = view.findViewById(R.id.software_status);

        flagName = view.findViewById(R.id.flagreport_name);
    }
此外,每次调用api时,您都在构建改造实例,最好创建一个用于改造的单例,并调用create方法在片段或活动中初始化一个全局变量,然后在函数中使用此变量,如下所示

// Global variable
private WebApi api = Retrofit.getInstance().create(WebApi.class);
然后在任何函数调用中

api.getNameStatus();

如果我删除这一行,那么我的输出不会每5秒更新一次。问题是,在程序执行的每一次自动更新中,而不是更新当前文本视图时,我都会添加另一组“状态”。
hardwareStatus.append(“\n\n”)这行代码为我的每个状态提供一个新行,否则它将被水平设置,没有空格,
Fragment
不提供
findViewById()
方法。这在
活动
视图
中提供。当实现一个
片段
时,你不会在
onCreate()
中膨胀你的视图(就像你在活动中通常做的那样)。相反,你在
onCreateView()中膨胀,你需要使用膨胀的根视图在你膨胀的布局中找到ID。@YenBico你说的“它增加了另一组”状态是什么意思" ? 发生这种情况是完全正常的,因为您要将新响应附加到textview的旧内容中,您需要更清楚地指定您需要的是什么need@YenBico我知道Fragment类不提供findViewById()方法,这就是为什么我使用像这个视图一样创建的onview中传递的view参数。findViewByDi如果我删除了那一行,那么我的输出不会每5秒更新一次。问题是,在程序执行的每一次自动更新中,而不是更新当前文本视图时,我都会添加另一组“状态”。
api.getNameStatus();