ListView控件没有出现在应用程序窗口小部件数据 [英] ListView not showing data in App Widget
问题描述
我想实现我的应用程序的简单集合小部件,我试图表现出的ListView
一个小部件,但里面的的ListView
是停留在加载中,从来没有完成加载,和我有数据库中的数据和列表视图中还是坚持了下来。
I'm trying to implement a simple collection widget for my application, i'm trying to show a ListView
inside a widget but the ListView
is stuck at Loading... and never finish loading, and i have data in the database and the listview is still stuck with it.
下面是从我AppWidgetProvider类的onupdate
方法:
Here is the onUpdate
method from my AppWidgetProvider class:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int length = appWidgetIds.length;
for (int x = 0; x < length; x++){
RemoteViews remote = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent intent = new Intent(context,RViewsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[x]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
remote.setRemoteAdapter(R.id.list,intent);
remote.setEmptyView(R.id.list,R.id.empty);
remote.setTextViewText(R.id.title,Utilities.setDayOfWeek());
////////////////////////////////////////////////////////////////////////////////////////////////////////
Intent AddmateriaIntent = new Intent(context,AddMateria.class);
PendingIntent pendingItentForButtonAddMateria = PendingIntent.getActivity(context,0,AddmateriaIntent,0);
remote.setOnClickPendingIntent(R.id.add,pendingItentForButtonAddMateria);
remote.setOnClickPendingIntent(R.id.empty, pendingItentForButtonAddMateria);
appWidgetManager.updateAppWidget(appWidgetIds[x],remote);
}
super.onUpdate(context,appWidgetManager,appWidgetIds);
}
我可以检查和方法的onupdate
无差错完成自己的工作。
这里是RemoteViewService和RemoteViewsFactory他们也没有在logcat中显示错误。
I could check and the method onUpdate
finish its job without errors.
And here is the RemoteViewService and RemoteViewsFactory they also don't show error on logCat.
公共类RViewsService扩展RemoteViewsService {
public class RViewsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new RViewFactory(getApplicationContext(),intent);
}
class RViewFactory implements RemoteViewsFactory {
private DBAdapter adapter;
private ArrayList<Materia> materias;
private Context context;
public RViewFactory(Context context,Intent intent){
this.context = context;
}
@Override
public void onCreate() {
adapter = new DBAdapter(context).openReadableDatabase();
materias = new ArrayList<Materia>();
Cursor cursor = adapter.query(Utilities.setDayAndTable(), new String[]{DBAdapter.ROW_SUBJECT,DBAdapter.ROW_COLOR_NAME,DBAdapter.ROW_COLOR_HEX}, null, null, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
Materia materia = new Materia();
materia.setSubject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_SUBJECT)));
materia.setColor(new ColorObject(cursor.getString(cursor.getColumnIndex(DBAdapter.ROW_COLOR_NAME)),
(cursor.getInt(cursor.getColumnIndex(DBAdapter.ROW_COLOR_HEX)))));
materias.add(materia);
Log.d("log", "Data: " + materia.getSubject());
cursor.moveToNext();
}
}
cursor.close();
}
}
@Override
public void onDataSetChanged() {
}
@Override
public void onDestroy() {
materias = null;
adapter.close();
}
@Override
public int getCount() {
return materias.size();
}
@Override
public RemoteViews getViewAt(int i) {
RemoteViews remote = new RemoteViews(context.getPackageName(),android.R.layout.simple_list_item_1);
remote.setTextViewText(android.R.id.text1,materias.get(i).getSubject());
remote.setTextColor(android.R.id.text1,materias.get(i).getColor().getColor());
return remote;
}
@Override
public RemoteViews getLoadingView() {
return null;
}
@Override
public int getViewTypeCount() {
return 0;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public boolean hasStableIds() {
return false;
}
}
}
而在LogCat中我找不到错误或警告时,Widget是加载,它只有当光标被读取显示(我已经删除从LogCat中的文本,因为它是对GC)。
And in the LogCat i cant find error or warnings when the Widget is loading, it only show when the cursor is being read (i've erased text from the LogCat because it was about the GC).
的logcat
05-02 12:54:30.796 21885-21885/schooltimetable.app D/cannon﹕ Data: Math
05-02 12:54:30.806 21885-21885/schooltimetable.app D/cannon﹕ Data: Theory
和我敢肯定,光标返回去的数据,因为我在logcat中记录这些东西,也如上图所示。
And i'm sure that the Cursor returns de data because i've log it in Logcat and it did as shown above.
这是清单片段声明AppWidgetProvider和RemoteViewService:
And this is the Manifest snippet declaring the AppWidgetProvider and RemoteViewService:
<receiver android:name="schooltimetable.app.appWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/cannon_widget" />
</receiver>
<service android:name=".RViewsService" android:permission="android.permission.BIND_REMOTEVIEWS"/>
我测试在的Nexus 7(2012年),我的应用程序:Android的4.4.2
感谢。
推荐答案
修改 getViewTypeCount
返回计数为1。
getViewTypeCount
是类型的意见将由工厂返回的数量。
getViewTypeCount
is the number of types of Views that will be returned by the factory.
@Override
public int getViewTypeCount() {
return 1;
}
这篇关于ListView控件没有出现在应用程序窗口小部件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!