棉花糖升级后,appwidget listview 行未更新或加载
Posted
技术标签:
【中文标题】棉花糖升级后,appwidget listview 行未更新或加载【英文标题】:appwidget listview rows not updating or loading after Marshmallow upgrade 【发布时间】:2016-01-23 21:15:14 【问题描述】:我有一个小部件应用程序,可以在 android Lollipop 或更低的操作系统上正常运行。当我将我的 nexus 5 升级到 Marshmallow (android 6.0) 时,我意识到我的小部件在加载自定义行列表视图时出现问题。 Widget 有一个动态的 textView 和一个动态的 listView。 textView 完美运行;但 listView 行项目没有希望。当我添加小部件时,行卡在加载阶段。在 Lollipop 或更低的设备上运行的代码完全相同,但在 Marshmallow 上却没有。我相信 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:(棉花糖上的关键字返回 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 行问题:
【问题讨论】:
认为链接是源代码。嗯。没有原始代码很难修复安卓应用。 【参考方案1】:我发现了我犯的错误。我有一个带有样式android:style/Widget.Material.Button
的按钮的自定义列表视图。当我删除按钮的样式时,它解决了我的问题。
<Button
android:id="@+id/widget_button"
android:layout_
android:layout_
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>
【讨论】:
【参考方案2】:在我的例子中,我在列表视图项布局中使用了 androidx 类型的小部件,例如 androidx.appcompat.widget.AppCompatTextView。更改为 TextView 并解决了问题。
【讨论】:
以上是关于棉花糖升级后,appwidget listview 行未更新或加载的主要内容,如果未能解决你的问题,请参考以下文章