ListView not showing data in App Widget

亡梦爱人 提交于 2019-12-02 05:29:45

问题


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.

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);
}

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.

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;
    }
}

}

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

And i'm sure that the Cursor returns de data because i've log it in Logcat and it did as shown above.

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"/>

I'm testing my app on Nexus 7 (2012) : Android 4.4.2

Thanks.


回答1:


Change the return count of getViewTypeCount to 1.

getViewTypeCount is the number of types of Views that will be returned by the factory.

 @Override
    public int getViewTypeCount() {
        return 1;
    }



回答2:


I had same problem because I put custom view in list row, try to remove them if you have.



来源:https://stackoverflow.com/questions/22818300/listview-not-showing-data-in-app-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!