1

私はdeveloper.android.comの指示に従うために最善を尽くしましたが、私が理解できない愚かなエラーがどこかにあるようです.

継続的にログを記録した後、コンテンツ プロバイダー カーソルからのデータを使用してウィジェットに ListView を設定する (試行する) RemoteViewsFactory の実装で、どのメソッドも実行されないことに気付きました。

RemoteViewsFactory :

private Context context;
private int appWidgetId;
private Cursor cursor;

public FootballScoreFactory(Context context, Intent intent){
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

}

@Override
public void onDataSetChanged() {

    if(cursor != null){
        cursor.close();
    }

    String[] date = new String[1];
    Date filterDate = new Date(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    date[0] = format.format(filterDate);
    try {
        cursor = context.getContentResolver().query(
                Uri.parse([content uri here, tested, returns the data]),
                null,
                null,
                date,
                null
        );
    } catch (Exception e){
        e.printStackTrace();
    }

}
@Override
public RemoteViews getViewAt(int position) {

    String leagueName, home, away, homeGoal, awayGoal;
    leagueName = home = away = homeGoal = awayGoal = "";

    if(cursor.moveToPosition(position)){
        leagueName = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.LEAGUE_COL));
        home = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.HOME_COL));
        away = cursor.getString(cursor.getColumnIndex(DatabaseContract.scores_table.AWAY_COL));
        homeGoal = String.valueOf(cursor.getInt(cursor.getColumnIndex(DatabaseContract.scores_table.HOME_GOALS_COL)));
        awayGoal = String.valueOf(cursor.getInt(cursor.getColumnIndex(DatabaseContract.scores_table.AWAY_GOALS_COL)));
    }

    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_list_item);
    rv.setTextViewText(R.id.league_name, leagueName);
    rv.setTextViewText(R.id.home, home);
    rv.setTextViewText(R.id.away, away);
    rv.setTextViewText(R.id.home_score, homeGoal.contentEquals("-1")? "-" : homeGoal);
    rv.setTextViewText(R.id.away_score, awayGoal.contentEquals("-1")? "-" : awayGoal);
    return rv;
}

RemoteViewsService :

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new FootballScoreFactory(getApplicationContext(), intent);
}

AppWidgetProvider:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    for (int appWidgetId : appWidgetIds) {
        Intent intent = new Intent(context, FootballScoreService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.football_score);
        rv.setRemoteAdapter(appWidgetId, R.id.widget_list_view, intent);
        rv.setEmptyView(R.id.widget_list_view, R.id.empty_view);

        appWidgetManager.updateAppWidget(appWidgetId, rv);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

ビューが確実に更新されるようにするために何をする必要があるかについての助けは素晴らしいでしょう。それが役立つ場合、 はrv.setEmptyView(...);常に機能しているようです。

そして、これで私が得た最大の失恋は、アプリマニフェストファイルでビューをバインドしていないと思ったのに、バインドしたように見えたときです。:(

<service android:name=".FootballScoreService"
        android:permission="android.permission.BIND_REMOTEVIEWS" />
4

1 に答える 1

3

それで、私は自分の問題を理解したことがわかりました。私の RemoteViewsFactory が getViewTypeCount() に対して正しい値を返さないのと同じくらい簡単でした。最初は0のままでした。1に変更したらうまくいきました。

続けて、私を笑ってください。やった。うまくいけば、これは一部の貧しい人々に役立ちます唯一ある時点で自分のような魂。

これにまた出くわしても驚かないでしょう笑。

于 2016-02-28T02:07:06.110 に答える