Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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/2/powershell/12.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 如何将对象传递到主屏幕小部件?_Java_Android_Xml_Android Widget - Fatal编程技术网

Java 如何将对象传递到主屏幕小部件?

Java 如何将对象传递到主屏幕小部件?,java,android,xml,android-widget,Java,Android,Xml,Android Widget,这是我第一次问这样的问题 我想将我的计算对象传递给要在主屏幕上显示的小部件。单击“计算”按钮时,我在MainActivity.java中有要更新的小部件: 我在用包裹机 我的问题是小部件没有显示从提供者类传递给它的计算信息。它似乎在显示计算的日志中正确地分组,只是我可能错误地设置了小部件视图以接收数据并显示它 public void saveCalcInfo(View v){ [...] setWidget(calculation); } pr

这是我第一次问这样的问题

我想将我的计算对象传递给要在主屏幕上显示的小部件。单击“计算”按钮时,我在MainActivity.java中有要更新的小部件:

我在用包裹机

我的问题是小部件没有显示从提供者类传递给它的计算信息。它似乎在显示计算的日志中正确地分组,只是我可能错误地设置了小部件视图以接收数据并显示它

public void saveCalcInfo(View v){
     
     [...]
     
     setWidget(calculation);
}


private void setWidget(Calculation calculationToPassToWidget) {
     BullAppWidgetProvider.manualUpdateWidgets(getApplicationContext(), calculationToPassToWidget);
}

我真正需要的是从小部件接收计算信息,并显示它。 单击“查看”按钮,启动MainActivity,并用显示的计算信息自动填充EditText,这是一个很好的选择

这是我的BullAppWidgetProvider.java 如果需要,这里是

widget_layout.xml 参考文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="@color/primary">


    <TextView
        android:id="@+id/symbol"
        style="@style/AdapterStyle"
        android:text="@string/ipl" />

    <TextView
        android:id="@+id/shares"
        style="@style/AdapterStyle"
        android:layout_alignParentEnd="true"
        android:text="@string/_42" />

    <TextView
        android:id="@+id/buy"
        style="@style/AdapterStyle"
        android:layout_below="@id/symbol"
        android:text="@string/buy" />

    <TextView
        android:id="@+id/sell"
        style="@style/AdapterStyle"
        android:layout_below="@id/buy"
        android:text="@string/sell" />

    <TextView
        android:id="@+id/comm"
        style="@style/AdapterStyle"
        android:layout_below="@id/shares"
        android:layout_alignParentEnd="true"
        android:text="@string/comm" />

    <Button
        android:id="@+id/viewCalcBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sell"
        android:layout_alignParentEnd="true"
        android:text="@string/view" />
</RelativeLayout>


以及

widget_provider.xml

<
android:id=“@+id/ll\u父对象\u小部件”
android:layout\u width=“匹配父项”
android:layout\u height=“match\u parent”/>

谢谢你抽出时间

嗨,迈克,谢谢你的回复。我编辑并希望澄清我的问题。如果需要进一步细分,请告诉我。我只是不知道此时的故障点在哪里。我只希望小部件中的一个项目在单击“计算”时被覆盖,它使用小部件的布局格式。但模板布局只有在我开始使用widget_provider.xml中的ListView时才开始显示。为了响应您关于存储的消息,我使用Firebase Firestore数据库将计算保存到cloud,然后在MainActivity>的下部RecyclerView中调用它们,填充上部的EditText。在这种情况下,我不知道如何将它们检索到一个小部件中(或者如果这更容易),但我现在正试图从活动中手动传递它们。是的,明白了,首先要坚持让它工作。这要容易得多,但是有很多不必要的东西。您可以去掉
RemoteViewService
/
BullWidgetItemFactory
,以及它的
元素。同时从
BullAppWidgetProvider
中删除远程
适配器设置。然后将
widget\u提供程序中的
替换为
widget\u布局中的
,然后可以将其删除。我还不确定WidgetAppService的作用是什么,但我们现在不需要它。一般的更新看起来还可以,但是在所有的东西都被修剪之后,你会更容易看到它的关闭位置。这一点很好。因此,在我的项目中,我将删除整个RemoteViewService类,manifest标记delete setRemoteAdapter。我是否应该用你建议的内容编辑这个问题,这样你就可以更容易地看到它?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:background="@color/primary">


    <TextView
        android:id="@+id/symbol"
        style="@style/AdapterStyle"
        android:text="@string/ipl" />

    <TextView
        android:id="@+id/shares"
        style="@style/AdapterStyle"
        android:layout_alignParentEnd="true"
        android:text="@string/_42" />

    <TextView
        android:id="@+id/buy"
        style="@style/AdapterStyle"
        android:layout_below="@id/symbol"
        android:text="@string/buy" />

    <TextView
        android:id="@+id/sell"
        style="@style/AdapterStyle"
        android:layout_below="@id/buy"
        android:text="@string/sell" />

    <TextView
        android:id="@+id/comm"
        style="@style/AdapterStyle"
        android:layout_below="@id/shares"
        android:layout_alignParentEnd="true"
        android:text="@string/comm" />

    <Button
        android:id="@+id/viewCalcBT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/sell"
        android:layout_alignParentEnd="true"
        android:text="@string/view" />
</RelativeLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/widget_app_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/app_name" />

    <
        android:id="@+id/ll_parent_for_widget"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>