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

时间:2017-04-11 01:21:53

标签: android

我目前正在创建一个新项目,其中包含一个包含在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();
}

1 个答案:

答案 0 :(得分:0)

@KeithB

我写了Singleton Shared Preferences类。它可能对你有用

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

有关详细信息,请参阅链接SharedPref Sample

相关问题