在棉花糖升级后,appwidget listview行不会更新或加载

时间:2015-10-24 11:13:22

标签: android android-widget android-appwidget android-6.0-marshmallow appwidgetprovider

我有一个小部件应用程序可以正常使用Android Lollipop或更低的操作系统。当我将nexus 5升级到Marshmallow(android 6.0)时,我意识到我的小部件在加载自定义行列表视图时遇到问题。 Widget有一个动态textView和一个动态listView。 textView工作得很好;但是listView行项目没有希望。当我添加小部件时,行处于加载阶段。它与Lollipop或更低版本的设备完全相同,但不适用于Marshmallow。我相信棉花糖正在引发一个我无法找到它的问题。我希望有人告诉我我做错了什么并帮助我解决这个问题。我很感激所有的帮助。谢谢。

WidgetProvider.class:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        System.out.println("WidgetProvider.onUpdate()");

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        final boolean isMaviKart = MainUtils.getCardType(context).equals(Constants.CARD_TYPE_MAVI);

        try {
            //set widget id
            ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
            MainUtils.setWidgetID(prefs, allWidgetIds[allWidgetIds.length - 1]);

            for (int i = 0; i < appWidgetIds.length; i++) {
                Intent intentService = new Intent(context, WidgetService.class);

                intentService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
                intentService.setData(Uri.parse(intentService.toUri(Intent.URI_INTENT_SCHEME)));

                RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
                widget.setRemoteAdapter(appWidgetIds[i], R.id.list_view, intentService);

                Intent clickIntent = new Intent(context, MainActivity.class);
                PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                widget.setPendingIntentTemplate(R.id.list_view, clickPI);

                if (isMaviKart) {
                    widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTokenPass(prefs)));
                    widget.setTextViewText(R.id.currency_tl, " Geçiş");
                }
                else {
                    widget.setTextViewText(R.id.kalanPara, String.valueOf(MainUtils.getTotalMoney(prefs)));
                    widget.setTextViewText(R.id.currency_tl, " TL");
                }

                appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, context.getResources().getString(R.string.error_occured), Toast.LENGTH_SHORT).show();
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        System.out.println("WidgetProvider.onReceive()");

        String key = intent.getStringExtra(Constants.EXTRA);
        if (key != null)
            Log.e("TEST", key);

    }

WidgetViewsFactory.class:

@Override
    public RemoteViews getViewAt(int position) {

        Intent intent = new Intent();
        Bundle extras = new Bundle();

        rowButton = context.getResources().getIdentifier("widget_button","id", context.getPackageName());
        rowText = context.getResources().getIdentifier("widget_textview","id", context.getPackageName());

        RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row);

        // set BUTTON NAMES
        row.setTextViewText(rowButton, getFeeTypes(position));


        // simple rows:
        configureRowsInListview(row, position, context.getResources().getColor(R.color.DeepSkyBlue), View.VISIBLE);




        extras.putString(Constants.EXTRA, getEventKey(position));
        intent.putExtras(extras);
        row.setOnClickFillInIntent(rowButton, intent);
        row.setOnClickFillInIntent(rowText, intent);

        return row;
    }

WidgetService.class:

public class WidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return(new WidgetViewsFactory(this.getApplicationContext()));
    }
}

MainActivity.class :( keyWord在Marshmallow上返回null)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    String keyWord = getIntent().getStringExtra(Constants.EXTRA);
    if (keyWord == null) {
        keyWord = "null";
        Log.e("onCreate() ", "No data Arrived yet!");
    }
}

 private void updateWidgetScreen(String updateData, String currencyType) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
        ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
        remoteViews.setTextViewText(R.id.kalanPara, updateData);
        remoteViews.setTextViewText(R.id.currency_tl, currencyType);

        Intent intent = new Intent(this, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);

        Intent clickIntent = new Intent(this, MainActivity.class);
        PendingIntent clickPI = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);

        Log.e("updateWidgetScreen", updateData+" updateData - widgetid: "+ MainUtils.getWidgetID(prefs));
    }

ListView Row问题: enter image description here

2 个答案:

答案 0 :(得分:0)

我发现了我犯的错误。我有一个带有样式android:style/Widget.Material.Button的按钮的自定义列表视图。当我删除Button的样式时,它解决了我的问题。

   <Button
            android:id="@+id/widget_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:padding="8dp"
            android:textColor="@color/White"
            **style="@style/MaterialButton"**
            />

<style name="MaterialButton" parent="android:style/Widget.Material.Button">
       <item name="android:background">@drawable/raised_button_background</item>
</style>

答案 1 :(得分:0)

在我的情况下,我在列表视图项布局中使用了androidx类型的小部件,例如androidx.appcompat.widget.AppCompatTextView。更改为TextView并解决了该问题。