2014-06-14 53 views
0

我作出這樣的包含4 TextView的我的應用程序的窗口小部件Android部件TextView的

這4個TextView中應得到sharepreferences

小部件做工精細的文字,但問題與獲取數據

這是他們說的loadpreferences方法Widget類代碼

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.content.pm.PackageManager; 
import android.content.pm.PackageManager.NameNotFoundException; 
import android.widget.RemoteViews; 
import android.widget.*; 
import android.content.*; 
import android.preference.*; 
import android.gesture.*; 

public class Widget extends AppWidgetProvider{ 
TextView b95,b98,mred,mgreen; 

public void onReceive(Context context, Intent intent) 
{ 

    LoadPreferences(); 
    String action = intent.getAction(); 
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) 
    { 

     RemoteViews views = new RemoteViews(context.getPackageName(), 
              R.layout.widget); 

     AppWidgetManager 
      .getInstance(context) 
      .updateAppWidget(
      intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), 
      views); 
    } 

} 
private void LoadPreferences() 
{ 
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String p95 = sharedPreferences.getString("p2p95", ""); 
    String p98 = sharedPreferences.getString("p2p98", ""); 
    String pred = sharedPreferences.getString("p2pred", ""); 
    String pgreen = sharedPreferences.getString("p2pgreen", ""); 

    b95.setText(p95); 
    b98.setText(p98); 
    mred.setText(pred); 
    mgreen.setText(pgreen); 

} 
} 

首先我有(這)個問題我不能用它

其次,我猜所有負載的喜好是錯誤的與小部件

那麼該如何解決

任何幫助使用它?

回答

0
String Harneet="hi"; 
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("Name",Harneet); 
    editor.commit(); 

後你想下面的代碼值使用

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
    String p95 = sharedPreferences.getString("p2p95", "Harneet");