2016-10-04 11 views
1

私はリストビューでappwidgetを作成します。 でRemoteViewsFactoryクラス私はSQLiteデータベースからのリストを設定しています。すべては大丈夫ですが、私はRemoteViewsFactorygetViewAt方法ではIndexOutOfBoundsExceptionを取得するよりも、1つの文字列でupdateWidget()メソッドを置く場合。なぜ私はRemoteViewsFactoryでIndexOutOfBoundsExceptionを取得していますか?

例外が発生する理由はありません。のでupdateWidget()メソッド私は1つのリモートビューにテキストを設定します。私が知られている溶液を用いてこれを行って

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) 
{ 
    SharedPreferences sPreferences = context.getSharedPreferences("WidgetSettings_"+appWidgetId,0); 
    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.initial_layout); 
    DBHelper dbHelper = DBHelper.getInstance(context); 

    // To avoid Exception in a RemoteViewsFactory.getViewAt() method just comment or delete this line 
    rv.setTextViewText(R.id.textViewListTotal, "Total sum of items"); // this line cause an Exception. If I commenting it then all is OK 


    final Intent intent = new Intent(context, WidgetRemoteViewsService.class); 
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
    rv.setRemoteAdapter(appWidgetId, R.id.items_list, intent); 

    rv.setEmptyView(R.id.items_list, R.id.empty_view); 

    final Intent actionIntent = new Intent(context, WidgetProvider.class); 
    actionIntent.setAction(WidgetProvider.ACTION_MANAGE); 
    actionIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    actionIntent.setData(Uri.parse(actionIntent.toUri(Intent.URI_INTENT_SCHEME))); 
    PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, actionIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    rv.setPendingIntentTemplate(R.id.items_list, actionPendingIntent); 

    appWidgetManager.updateAppWidget(appWidgetId, rv); 
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.items_list); 
} 

答えて

0

:問題を引き起こす

ここでは、コードリストと文字列。

@Override 
public RemoteViews getViewAt(int position) { 

    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.list_item); 

    try { 
     if(ListItems.size() > 0) //<------- avoid indexoutofbound exception 
     { 
      // doing something with a remoteviews 
     } 
    } 
    catch (IndexOutOfBoundsException e) 
    { 
     e.printStackTrace(); 
    } 

    return rv; 
} 
+1

私はバグがそれが何であるかを知らない:getViewAt方法で私はこのようなチェック文が貼り付けます。誰かが知っている場合は、ソリューションを投稿してください – Trancer

関連する問題