如何从preferenceActivity中选择检索

时间:2012-12-20 14:18:09

标签: preferenceactivity

(Android 2.2)

我正在尝试检索PreferenceActivity中的值集。尽管活动本身正确存储/调用了该值,但我无法弄清楚如何自己检索设置值(例如在textView中显示它)。此代码不会出错,但值为0显示在Spel.java中的textView1中,而不是PrefsManager prefs中的实际/当前值(在NumberPickerPreferenceActivity中)。

如何在textView1中显示正确的值?

另外:onSharedPreferenceChanged(在NumberPickerPreferenceActivity中)没有显示toast messsage(没有弹出任何内容,没有记录任何内容)。有什么不对,怎么解决?

这是NumberPickerPreferenceActivity类:

package mypackage.tabnavui;

import mypackage.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class NumberPickerPreferenceActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    private static final String TAG = NumberPickerPreferenceActivity.class.getSimpleName();
    private SharedPreferences prefs;
    public static int aantalBanen;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PreferenceManager preferenceManager = getPreferenceManager();
        preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
        preferenceManager.setSharedPreferencesName("numberPicker.preferences");

        addPreferencesFromResource(R.xml.preferences);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    public int prefsAantalBanen() {
        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        return prefs.getInt( "AANTAL_BANEN", 0 );
    }

    public int getAantalBanen() {
        aantalBanen = prefsAantalBanen();
        return aantalBanen;
    }

    @Override
    protected void onResume() {
        super.onResume();
        prefs.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {      
        super.onPause();
        prefs.unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        int selectedNumber = prefsAantalBanen();
        Log.d(TAG, "Number selected: " + selectedNumber);
        Toast t = Toast.makeText(this, "Number selected: " + selectedNumber, Toast.LENGTH_SHORT);
        t.show();
    }    


}

这里是Spel.java,我想在ReferenceActivity中显示选择/存储的值,请参阅方法toonClickHandler:

    package mypackage.tabnavui;

    import mypackage.tabnavui.R;
import mypackage.Controls2.MyGestureDetector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


    public class Spel extends Activity implements NumberPickerDialog.OnNumberSetListener {
        private static final String TAG = Spel.class.getSimpleName();

        private GestureDetector gestureDetector;
        private TextView textView1; 

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            gestureDetector = new GestureDetector(new MyGestureDetector());

            // Use a custom layout file
            setContentView(R.layout.spel);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_sample_picker, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (item.getItemId() == R.id.menu_dialog_item) {
                NumberPickerDialog dialog = new NumberPickerDialog(this, -1, 2);
                dialog.setTitle(getString(R.string.dialog_picker_title));
                dialog.setOnNumberSetListener(this);
                dialog.show();

                return true;
            } else if (item.getItemId() == R.id.menu_preferences_item) {
                startActivity(new Intent(this, NumberPickerPreferenceActivity.class));
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        public void toonClickHandler(View v) {
            textView1 = (TextView) this.findViewById(R.id.textView1);
            int aantalBanen = NumberPickerPreferenceActivity.aantalBanen;
            textView1.setText(String.valueOf(aantalBanen));

        }

        public void onNumberSet(int number) {
            Log.d(TAG, "Number selected: " + number);
            Toast t = Toast.makeText(this, "Number selected: " + number, Toast.LENGTH_SHORT);
            t.show();
        }       

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        }


        class MyGestureDetector extends SimpleOnGestureListener {
              private static final int SWIPE_MAX_OFF_PATH = 200;
              private static final int SWIPE_MIN_DISTANCE = 50;
              private static final int SWIPE_THRESHOLD_VELOCITY = 200;

            @Override
            public boolean onDown (MotionEvent e) {
                return true;
            }

              @Override
              public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                   if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                       return false;
                   // left to right swipe and right to left swipe
                   if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        // left swipe
                        Toast t = Toast.makeText(Spel.this, "Left swipe", Toast.LENGTH_LONG);
                        t.show();
                        // startActivity(Tabs3.tab3);
                        startActivityForResult(Tabs3.tab3, 500);
                        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                        return true;
                   } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        // right swipe
                        Toast t = Toast.makeText(Spel.this, "Right swipe", Toast.LENGTH_LONG);
                        t.show();
                        // startActivity(Tabs3.tab1);
                        startActivityForResult(Tabs3.tab1, 500);
                        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                        return true;
                   }
                   return false;
              }    
        }

    }

和preferences.xml:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:picker="http://schemas.android.com/apk/res/mypackage.tabnavui"
                  xmlns:android="http://schemas.android.com/apk/res/android">

    <nl.computerhuys.tabnavui.NumberPickerPreference
            android:key="AANTAL_BANEN"
            android:title="Aantal banen"
            android:summary="Number picker as a preference"
            picker:defaultValue="2"
            picker:startRange="0"
            picker:endRange="12" />

</PreferenceScreen>

1 个答案:

答案 0 :(得分:0)

在此处找到:How do I get the SharedPreferences from a PreferenceActivity in Android?

NumberPickerReferenceActivity中的

:替换

preferenceManager.setSharedPreferencesMode(MODE_PRIVATE);

通过

preferenceManager.setSharedPreferencesMode(MODE_WORLD_READABLE);

并替换

prefs = PreferenceManager.getDefaultSharedPreferences(this);

通过

prefs = getSharedPreferences("numberPicker.preferences", MODE_WORLD_READABLE);

制作祝酒词和日志工作

和Spel:

int aantalBanen = getSharedPreferences("numberPicker.preferences", MODE_WORLD_READABLE).getInt( "AANTAL_BANEN", 0 );
textView1.setText(String.valueOf(aantalBanen));

在textView1中显示存储值