2010-12-13 8 views
7

다음은 간단한 클래스입니다. 위젯 TextView를 업데이트하지 않는 onReceive 메서드에서 문제가 있습니다. 그것은 setTextViewText 전에 라인에 출력되는 logcat의 올바른 정보를 보여줍니다. 나는 틀린 무엇이 확실하지 않고, 나의 머리를 끌어 당기고 있었다 (그리고 나는 벌써 대머리 다).setTextViewText가 위젯을 업데이트하지 않습니다.

public class SnowWidget extends AppWidgetProvider { 

public static List<Article> mymtns = new ArrayList<Article>(); 
public static RemoteViews remoteViews; 
public static ComponentName thisWidget; 

public static String amount = ""; 
public static String mtn_name = ""; 
public static String desc = ""; 
public static String condition = ""; 
public static String[] type; 

public static int index = 0; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
    int[] appWidgetIds) 
{ 

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); 

    thisWidget = new ComponentName(context, SnowWidget.class); 

    // This one works fine 
    remoteViews.setTextViewText(R.id.snowwidget, mtn_name+ "\n"+ amount+"\"\n"+ condition); 

    /* Make the buttons work */ 

Intent next = new Intent(context, SnowWidget.class); 
next.setAction(ACTION_WIDGET_RECEIVER); 

PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, next, 0); 
remoteViews.setOnClickPendingIntent(R.id.nextMtn, nextPendingIntent); 

/* END - Make the buttons work */ 

    appWidgetManager.updateAppWidget(thisWidget, remoteViews); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 

    // check, if our Action was called 
    if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
     if(mymtns.size() > 0) 
     { 

      // This show up correctly in logcat 
      Log.d("onReceive", "New Info => "+ mtn_name+ "\n"+ amount+"\"\n"+ condition); 
      // This never updates my widget 
      remoteViews.setTextViewText(R.id.snowwidget, mtn_name+ "\n"+ amount+"\"\n"+ condition); 

     } 
    } 

    super.onReceive(context, intent); 
} 

}

+1

업데이트에 대한 답변 – Brombomb

답변

20

는 답을 발견. remoteViews.setTextViewText을 호출 한 후 updateAppWidget을 호출하여 위젯을 업데이트해야합니다. 내가 추가 한 코드는 아래와 같습니다.

AppWidgetManager manager = AppWidgetManager.getInstance(context); 
manager.updateAppWidget(thisWidget, remoteViews); 
+0

감사합니다. 이 문제가 얼마나 많은 두통을 유발했는지 설명하는 것은 어렵습니다. –

+3

나는이 코드를 가지고 있으며 여전히 작동하지 않는다. 나는 무슨 일이 일어 났는지 전혀 모른다. – xdumaine

+0

몇 줄의 코드를 추가하고 있습니까? 내 위젯이 작동하지 않는 것 같습니다. 이 PendingIntent.getActivity (문맥, 0, 의도, 0) 대신 : PendingIntent.getActivity (문맥, 0, 의도, PendingIntent.FLAG_UPDATE_CURRENT) – Si8

관련 문제