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

标签 java android configuration android-widget sharedpreferences

Possible Duplicate:
Add several appWidgets with different configuration?

我有一个小部件,它显示一个简单的 TextView ,它可以在配置 Activity 中作为编辑文本字段进行编辑。我正在尝试使用不同的文本配置、fx.dll 添加多个小部件到主屏幕。一个小部件显示:“买一盏灯..”,另一个小部件显示:“我喜欢培根”。

我的问题是小部件一直显示相同的文本,如果我编辑一个小部件,另一个小部件也会发生变化。我使用共享首选项来保存输入的数据以供以后重新配置。 (我没有使用 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());

完整代码配置 Activity 代码:

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();

    }



}

最佳答案

哦,太酷了,只需将 appWidgetId 设置为 awID 即可。或者更简单,将 awID 传递给您调用共享首选项的方法。这应该可以解决问题。

关于java - 为不同的 appWidget 获取不同的共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12981442/

相关文章:

java - 制作一个compareTo覆盖失败,简单的类

java - 如何断言Java中的执行路径不会执行多次?

java - 横向模式在 Android Studio 模拟器中不起作用?

android - 什么时候允许将 Context 转换到 Activity?

configuration - 如何在 EMR 集群 Bootstrap 上设置 livy.server.session.timeout?

java - 公共(public)文件上传(Apache)

Java Xpath 排除 xml 中的子项

java - Observable vs Flowable rxJava2

sql-server - 如何根据作业名称批量更新 SQL Server 代理作业重试尝试

c# - 自定义 web.config 部分处理程序