0

重複の可能性:
構成が異なるいくつかの appWidgets を追加しますか?

構成アクティビティで edittextfield として編集可能な単純なテキストビューを表示するウィジェットがあります。異なるテキスト構成 fx を使用して、ホーム画面に複数のウィジェットを追加できるようにしようとしています。1 つのウィジェット表示: 「ランプを購入してください..」と別のウィジェット表示:「I love Bacon」。

私の問題は、ウィジェットが同じテキストを表示し続け、1 つのウィジェットを編集すると他のウィジェットも変更されることです。後で再構成するために、入力されたデータを保存するために sharedpreferences を使用しています。(私は 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();

    }



}
4

1 に答える 1

1

appWidgetId を awID に設定するだけです。またはさらに簡単に、sharedPreferences を呼び出しているメソッドに awID を渡します。それはそれを修正する必要があります。

于 2012-10-19T20:44:14.680 に答える