Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 共享首选项:检索并显示存储的数据_Android_Sharedpreferences - Fatal编程技术网

Android 共享首选项:检索并显示存储的数据

Android 共享首选项:检索并显示存储的数据,android,sharedpreferences,Android,Sharedpreferences,我已利用共享首选项将数据存储到MyUserChoice.xml中: <string name="MyUserChoice">ApplicationInfo{1ebad2cb com.example.user.example}, ApplicationInfo{15c7caa8 com.android.gallery}, ApplicationInfo{bc0a9c1 com.android.quicksearchbox}</string> 相反,它不返回

我已利用共享首选项将数据存储到MyUserChoice.xml中:

<string name="MyUserChoice">ApplicationInfo{1ebad2cb com.example.user.example},
    ApplicationInfo{15c7caa8 com.android.gallery},
    ApplicationInfo{bc0a9c1 com.android.quicksearchbox}</string>
相反,它不返回任何内容,我在日志中看到:

10-09 01:19:18.756    1311-1311/com.android.systemui W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
10-09 01:19:18.756    1311-1311/com.android.systemui W/PackageManager﹕ Failure retrieving resources for com.example.checkboxsharedpreferences: Resource ID #0x0
10-09 01:19:18.781    1311-1327/com.android.systemui I/art﹕ Background sticky concurrent mark sweep GC freed 10643(410KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 22MB/22MB, paused 5.699ms total 16.577ms
10-09 01:19:19.371      931-931/? W/SurfaceFlinger﹕ couldn't log to binary event log: overflow.
有人知道我的编码有什么问题吗?特此附上我的MainActivity.java:

ListView myList;
Button getChoice, clearAll, selectAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myList = (ListView)findViewById(android.R.id.list);
    getChoice = (Button)findViewById(R.id.getchoice);
    clearAll = (Button)findViewById(R.id.clearall);
    selectAll = (Button)findViewById(R.id.selectall);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(adapter);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if(sharedpreferences.contains(MyPREFERENCES)){
        LoadSelections();
    }

    getChoice.setOnClickListener(new Button.OnClickListener() {


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

            }

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

        }
    });

    clearAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ClearSelections();
        }
    });

    selectAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SelectAllSelections();
        }
    });

    packageManager = getPackageManager();

    new LoadApplications().execute();
   }
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(),
                        "Current 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();
}

private void SelectAllSelections() {
// 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, true);
    }
// also clear the saved selections then uncomment the below line.
// SaveSelections();
}

protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);

    try{
        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        /*if(intent != null){
            startActivity(intent);
        }*/
    }catch(ActivityNotFoundException e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }catch(Exception e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
    ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
    for(ApplicationInfo info : list){
        try{
            if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
                appList.add(info);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return appList;
}

private class LoadApplications extends AsyncTask<Void, Void, Void>{
    private ProgressDialog progress = null;

    protected Void doInBackground(Void... params){
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
        return null;
    }
    protected void onPostExecute(Void result){
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(result);
    }
    protected void onPreExecute(){
        progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
        super.onPreExecute();
    }
}
}
ListView-myList;
按钮getChoice、clearAll、selectAll;
SharedReferences SharedReferences;
公共静态最终字符串MyPREFERENCES=“MyUserChoice”;
ArrayList selectedItems=新建ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList=(ListView)findViewById(android.R.id.list);
getChoice=(按钮)findViewById(R.id.getChoice);
clearAll=(按钮)findViewById(R.id.clearAll);
selectAll=(按钮)findViewById(R.id.selectAll);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u多选);
myList.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
myList.setAdapter(适配器);
SharedReferences=GetSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
if(SharedReferences.contains(MyPREFERENCES)){
LoadSelections();
}
getChoice.setOnClickListener(新建按钮.OnClickListener(){
@凌驾
公共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;i10-09 01:19:18.756    1311-1311/com.android.systemui W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
10-09 01:19:18.756    1311-1311/com.android.systemui W/PackageManager﹕ Failure retrieving resources for com.example.checkboxsharedpreferences: Resource ID #0x0
10-09 01:19:18.781    1311-1327/com.android.systemui I/art﹕ Background sticky concurrent mark sweep GC freed 10643(410KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 22MB/22MB, paused 5.699ms total 16.577ms
10-09 01:19:19.371      931-931/? W/SurfaceFlinger﹕ couldn't log to binary event log: overflow.
ListView myList;
Button getChoice, clearAll, selectAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myList = (ListView)findViewById(android.R.id.list);
    getChoice = (Button)findViewById(R.id.getchoice);
    clearAll = (Button)findViewById(R.id.clearall);
    selectAll = (Button)findViewById(R.id.selectall);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(adapter);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if(sharedpreferences.contains(MyPREFERENCES)){
        LoadSelections();
    }

    getChoice.setOnClickListener(new Button.OnClickListener() {


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

            }

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

        }
    });

    clearAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ClearSelections();
        }
    });

    selectAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SelectAllSelections();
        }
    });

    packageManager = getPackageManager();

    new LoadApplications().execute();
   }
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(),
                        "Current 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();
}

private void SelectAllSelections() {
// 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, true);
    }
// also clear the saved selections then uncomment the below line.
// SaveSelections();
}

protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);

    try{
        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        /*if(intent != null){
            startActivity(intent);
        }*/
    }catch(ActivityNotFoundException e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }catch(Exception e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
    ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
    for(ApplicationInfo info : list){
        try{
            if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
                appList.add(info);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return appList;
}

private class LoadApplications extends AsyncTask<Void, Void, Void>{
    private ProgressDialog progress = null;

    protected Void doInBackground(Void... params){
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
        return null;
    }
    protected void onPostExecute(Void result){
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(result);
    }
    protected void onPreExecute(){
        progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
        super.onPreExecute();
    }
}
}
SharedPreferences.Editor prefEditor = sharedpreferences.edit();
String savedItems = getSavedItems();
prefEditor.putString(MyPREFERENCES, savedItems);
prefEditor.commit();
SharedPreferences sharedpreferences= getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String channel = sharedpreferences.getString(MyPREFERENCES, "null");
sharedpreferences.contains(MyPREFERENCES.toString())
!sharedpreferences.getString(MyPREFERENCES, "").equals("")