0

我已尽全力按照developer.android.com上的指示行事,但它根本不起作用。在过去的几天里,我一直在这个过程中取得零进展。我知道我有一个愚蠢的错误,但我无法弄清楚。在连续日志记录之后,我意识到没有任何方法会在我实现RemoteViewsFactory的过程中运行(尝试)使用来自内容提供者游标的数据在小部件中设置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" /> 

预先感谢您。

回答

3

所以,事实证明,我想通了,我自己的问题,这是我的RemoteViewsFactory不返回的getViewTypeCount()正确的值一样简单。我把它忘在0在第一。当我把它改为1,它的工作。

下去,笑我,我做到了。希望这有助于一些贫困唯一灵魂像我这样在某个时候。

我不会如果我绊倒,会感到惊讶穿过这再次大声笑。

相关问题