Android 从可检查列表中检索共享首选项

Android 从可检查列表中检索共享首选项,android,Android,我目前正在创建一个新项目,其中包含一个array.xml中包含的项目列表的复选框。我正在使用共享首选项,希望能够在其他活动中调出选中的项目。我有一个保存选择并打开新活动的按钮。现在我只是在第二个活动中出现问题。我将显示我主要活动的代码,因为我不确定如何开始第二个活动 @Override public void onClick(View v) { String selected = ""; int cntChoice = myList.getCount(); Sparse

我目前正在创建一个新项目,其中包含一个array.xml中包含的项目列表的复选框。我正在使用共享首选项,希望能够在其他活动中调出选中的项目。我有一个保存选择并打开新活动的按钮。现在我只是在第二个活动中出现问题。我将显示我主要活动的代码,因为我不确定如何开始第二个活动

@Override
public void onClick(View v) {
    String selected = "";
    int cntChoice = myList.getCount();

    SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
    for(int i = 0; i < cntChoice; i++){
        if(sparseBooleanArray.get(i)) {
            selected += myList.getItemAtPosition(i).toString() + "\n";
            System.out.println("Checking list while adding:" + myList.getItemAtPosition(i).toString());
            SaveSelections();
            Intent learnintent = new 
            Intent(MainActivity.this,UserList.class);
            learnintent.putExtra("",selected);
            Toast.makeText(MainActivity.this, selected, 
            Toast.LENGTH_LONG).show();
            startActivity(learnintent);
        }
    }

    Toast.makeText(MainActivity.this, selected, 
    Toast.LENGTH_LONG).show();
    }});

    clearAll.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
        ClearSelections();
    }
});
}

private void SaveSelections() {
    // save the selections in the shared preference in private mode for the user

    SharedPreferences.Editor prefEditor = sharedpreferences.edit();
    String savedItems = getSavedItems();
    prefEditor.putString(MyPREFERENCES.toString(), savedItems);
    prefEditor.commit();
}

private String getSavedItems() {
    String savedItems = "";
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.myList.isItemChecked(i)) {
            if (savedItems.length() > 0) {
                savedItems += "," + this.myList.getItemAtPosition(i);
            } else {
                savedItems += this.myList.getItemAtPosition(i);
            }
        }
    }
    return savedItems;
}

private void LoadSelections() {
// if the selections were previously saved load them

    if (sharedpreferences.contains(MyPREFERENCES.toString())) {
        String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
        selectedItems.addAll(Arrays.asList(savedItems.split(",")));
        int count = this.myList.getAdapter().getCount();

        for (int i = 0; i < count; i++) {
            String currentItem = (String) myList.getAdapter().getItem(i);
            if (selectedItems.contains(currentItem)) {
                myList.setItemChecked(i, true);
                Toast.makeText(getApplicationContext(), "Curren Item: " + currentItem,Toast.LENGTH_LONG).show();
            } else {
                myList.setItemChecked(i, false);
            }
        }
    }
}

private void ClearSelections() {
    // user has clicked clear button so uncheck all the items
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myList.setItemChecked(i, false);
    }
    // also clear the saved selections
    SaveSelections();
}
@覆盖
公共void onClick(视图v){
所选字符串=”;
int cntChoice=myList.getCount();
SparseBooleanArray SparseBooleanArray=myList.getCheckedItemPositions();
for(int i=0;i0){
savedItems+=“,”+this.myList.getItemAtPosition(i);
}否则{
savedItems+=this.myList.getItemAtPosition(i);
}
}
}
返回savedItems;
}
私有void LoadSelections(){
//如果以前保存了选择,请加载它们
if(SharedReferences.contains(MyPREFERENCES.toString())){
String savedItems=SharedReferences.getString(MyPREFERENCES.toString(),“”);
选择editems.addAll(Arrays.asList(savedItems.split(“,”));
int count=this.myList.getAdapter().getCount();
for(int i=0;i
@KeithB

我编写了Singleton共享首选项类。它可能对你有用

  import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPref
{
    private static SharedPreferences mSharedPref;
    public static final String NAME = "NAME";
    public static final String AGE = "AGE";
    public static final String IS_SELECT = "IS_SELECT";

    public static void init(Context context)
    {
        if(mSharedPref == null)
            mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    }

    public static String read(String key, String defValue) {
        return mSharedPref.getString(key, defValue);
    }

    public static void write(String key, String value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putString(key, value);
        prefsEditor.commit();
    }

    public static boolean read(String key, boolean defValue) {
        return mSharedPref.getBoolean(key, defValue);
    }

    public static void write(String key, boolean value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putBoolean(key, value);
        prefsEditor.commit();
    }

    public static Integer read(String key, int defValue) {
        return mSharedPref.getInt(key, defValue);
    }

    public static void write(String key, Integer value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putInt(key, value).commit();
    }
}

有关更多信息,请参见链接

为共享首选项创建一个singleton类,这对Android来说是一个新的概念。如何实现singleton类?