2012-01-13 15 views
0

ウィジェット内のtextViewアイテムから値を取得する方法はありますか?ウィジェットのレイアウトに2つのtextViewsがあり、私のウィジェットのアップデートでは、それらのいずれかに新しい値を設定したいので、2番目の値を変更したくありません。私はこのAndroidがテキストを取得するウィジェット内の値を表示

remoteViews.setTextViewText(R.id.text1, newValue"); 
    widgetManager.updateAppWidget(thisWidget, remoteViews); 

のような値を設定したときに今のところ、これはレイアウトで定義された値をデフォルトに第二のTextViewの値を設定しています。 2番目のtextViewの値を変更しないようにする方法、または同じ値に調整できるように2番目のtextViewに値を取得する方法は?

答えて

3

それはonUpdateが呼ばれるたびに、それは私が最初にTextViewを設定するときに、私はsharedprefsへのTextViewのテキストを保存し、その後、私は時に好みを得るので、私は何が使用SharedPreferencesで、ウィジェットの全てを更新することを私の理解であります私は更新する。そして私はforループ内でこれをすべて行います。

は、ここに私のonUpdateです:私のクラスMで

@Override 
public void onUpdate(Context context, AppWidgetManager awm, int[] appWidgetIds){ 
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); 
    int appWidgetId; 
    for (int i=0;i<appWidgetIds.length;i++){ 
      appWidgetId = appWidgetIds[i]; 

      String widgetLabel = pref.getString("label"+appWidgetId, ""); 
      // I previously saved "widgetLabel" when I created the widget in my 
      // ConfigurationActivity 
      M.updateWidget(context, appWidgetId, widgetLabel); 
       // I put this in another class so it's more manageable. 
     } 



} 

public static void updateWidget(Context context, int appWidgetId, String widgetLabel){ 

     SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); 

     RemoteViews updateViews; 
     int layoutId = R.layout.layout; 
     int viewId = R.id.layout; 

     updateViews = new RemoteViews(context.getPackageName(), 
       layoutId); 
     updateViews.setTextViewText(R.id.widgetlabel, widgetLabel); 
     editor.putString("label"+appWidgetId, widgetLabel); 
     editor.commit(); 

     Intent intent = new Intent(context, ClickAction.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
     updateViews.setOnClickPendingIntent(viewId, pendingIntent); 

     AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, updateViews); 
    } 

私はそれが理にかなって願っています。私が何かを工夫する必要があるかどうかを教えてください。

+0

ありがとうございます!それは私のために働くはずです – Greg

関連する問題