为不同的appWidget获取不同的共享首选项

时间:2012-10-19 19:47:12

标签: java android configuration android-widget sharedpreferences

  

可能重复:
  Add several appWidgets with different configuration?

我有一个显示简单文本视图的小部件,可以在配置活动中编辑为edittextfield。我试图通过不同的文本配置fx将几个小部件添加到主屏幕。一个小部件显示:“买一盏灯......”,另一个说:“我喜欢培根”。

我的问题是小部件一直显示相同的文本,如果我编辑一个小部件,另一个小部件也会改变。我正在使用共享偏好来保存输入的数据,以便以后重新配置。 (我没有使用PreferenceActivity)

我几个小时以来一直在努力,任何帮助都会非常感激。

为什么这不起作用:

SharedPreferences sp;
EditText info;
String note;
int appWidgetId;

private void loadPrefs(){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
              , Context.MODE_PRIVATE);
        note = sp.getString("Note", "");

        info.setText(note);

    }

    private void savePrefs(String key, String value){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
                  , Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.putString("Note", info.getText().toString());
        editor.commit();   

    }

然后我有一个结束配置的确认按钮,它调用savePrefs方法

public void onClick(View v) {
        // TODO Auto-generated method stub

        savePrefs("Note", info.getText().toString());

完整代码配置活动代码:

import java.io.File;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class WidgetConfig extends Activity implements OnClickListener, OnItemSelectedListener {


    AppWidgetManager awm;
    int awID;
    Context context;
    EditText info;
    Button b;
    String note;
    int styleStart = -1, cursorLoc = 0;
    int appWidgetId;
    SharedPreferences sp;
    Spinner spinner;
    String[] paths = { "10", "20", "30" };
    File path = null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.widgetconfig);

        context = WidgetConfig.this;
        info = (EditText)findViewById(R.id.etwidgetconfig);


            ...


        b = (Button)findViewById(R.id.bwidgetconfig);
        loadPrefs();
        b.setOnClickListener(this);


        //Getting Info about the widget that launched this activity
        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null){
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID );
        }

        awm = AppWidgetManager.getInstance(context);

    }


        ...


    private void loadPrefs(){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
              , Context.MODE_PRIVATE);
        note = sp.getString("Note", "");

        info.setText(note);

    }

    private void savePrefs(String key, String value){
        sp = context.getSharedPreferences("widget" + String.valueOf(appWidgetId)
                  , Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.putString("Note", info.getText().toString());
        editor.commit();   

    }


    public void onClick(View v) {
        // TODO Auto-generated method stub

        savePrefs("Note", info.getText().toString());

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.tvConfigInput, info.getText());

        ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, views);

        Intent in = new Intent(context, WidgetConfig.class);
        PendingIntent pi = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);



        views.setOnClickPendingIntent(R.id.B_EditAgain, pi);

        awm.updateAppWidget(awID, views);


        Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
        setResult(RESULT_OK, result);
        finish();

    }



}

1 个答案:

答案 0 :(得分:1)

哦,很酷,所以只需将appWidgetId设置为awID即可。甚至更简单,将awID传递给您调用sharedPreferences的方法。那应该解决它。