2010-12-13 28 views
7

Aşağıda, sınıfımın basitleştirilmiş bir sürümü gösterilmiştir. TextView widget güncellenmeyen onReceive yönteminde sorun yaşıyorum. SetTextViewText'den önce satırda çıkarılan logcat içindeki doğru bilgileri gösterir. Neyin yanlış olduğunu ve saçlarımı çektiğimden emin değilim (ve şimdiden saçlarım).setTextViewText widget güncellenmiyor

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

Yanıtın güncelleştirilmesi – Brombomb

cevap

20

cevabı bulundu. remoteViews.setTextViewText numaralı telefonu aradıktan sonra, widget'ı updateAppWidget numaralı aramayla güncellemeniz gerekir. Eklediğim kod aşağıda gösterilmiştir.

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

Bunun için çok teşekkürler! Bir baş ağrısının ne kadarının bana bu problemin neden olduğunu anlatmak zor. –

+3

Bu koda sahibim ve hala çalışmıyor olmasına rağmen. Ne olduğuna dair hiçbir fikrim yok. – xdumaine

+0

Bu az sayıda kod satırını nereye ekliyorsunuz? Widget'ımın çalışmasını sağlayamıyorum. PendingIntent.getActivity (bağlam, 0, niyet, 0) yerine : PendingIntent.getActivity (bağlamda, 0, niyet, PendingIntent.FLAG_UPDATE_CURRENT) – Si8