【问题标题】:Fetch data from array and pass from widget to activity without listview从数组中获取数据并在没有列表视图的情况下从小部件传递到活动
【发布时间】:2018-07-10 15:36:31
【问题描述】:

Listview 是可滚动的,因此它不会通过动态调整其行大小来匹配父级。是否可以从数组中获取数据并将它们单独传递给活动,以便我可以改用LinearLayout

RemoteViews mainView = new RemoteViews(context.getPackageName(), R.layout.main_widget_layout);
    for (int i = 0; i < 16; i++) {
        RemoteViews textView = new RemoteViews(context.getPackageName(), R.layout.text_append_layout);
        textView.setTextViewText(R.id.appending_text, String.valueOf(i));
        mainView.addView(R.id.text_data_viewer, textView);

        Intent fillInIntent = new Intent();
        fillInIntent.putExtra("extradata", i);
        textView.setOnClickFillInIntent(R.id.appending_text, fillInIntent);
    }
Intent activitytoStart = new Intent(context, Widget.class).setAction("com.custom.action");
mainView.setPendingIntentTemplate(R.id.text_data_viewer, PendingIntent.getBroadcast(context, 0, activitytoStart, PendingIntent.FLAG_UPDATE_CURRENT));

ma​​in_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/text_data_viewer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="16"
                android:orientation="vertical"/>

text_append_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/appending_text"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center" />

【问题讨论】:

  • 根本没有人帮忙。几个月前,其他成员真的很有帮助。

标签: java android android-widget android-pendingintent android-appwidget


【解决方案1】:

首先,您为什么不使用 remoteviewsfactory?这将使填充列表视图变得更加容易。

但要回答您的问题 - 要将数据从小部件传递到活动,您需要在意图中传递它,因此当您单击小部件时,它会打开活动。 设置具有待处理意图的小部件:

        Intent clickIntentTemplate = new Intent(context, MainActivity.class);

        clickIntentTemplate.setAction(Intent.ACTION_MAIN);
        clickIntentTemplate.addCategory(Intent.CATEGORY_LAUNCHER);
        clickIntentTemplate.putExtra("extraName",extra);
        clickIntentTemplate.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, clickIntentTemplate, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent);

在活动中检索意图:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getIntent().getExtras() != null) {
        String retrievedString = getIntent().getExtras().getString("extraName");
    }
    //...rest of your code
}

在你的 onNewIntent() 中也包含该方法:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (getIntent().getExtras() != null) {
        String retrievedString = getIntent().getExtras().getString("extraName");
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-12
    • 2015-08-30
    • 2016-07-26
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2019-05-07
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多