Why My SharedPreference create another file name D

2019-09-06 02:27发布

I have a listview with choice mode, and it working. I want to save that checked item to shared preference, then use it in another activity. But, my SharedPreferences doesn'nt save my string correct, and save another file call DATA_Preferences that i never call in my code. The result is my Next activity get wrong value..

Here is my code that i use to save my string and call it use by another activity:

public void onClick(View v) {

            SparseBooleanArray checked = listView.getCheckedItemPositions();
            ArrayList<DBLokasi> selectedItems = new ArrayList<DBLokasi>();

            // Item position in adapter
            SharedPreferences prefs = getSharedPreferences("DATA_COOR", Context.MODE_PRIVATE);
            SharedPreferences.Editor prefsEditor = prefs.edit();

            for (int i = 0; i < checked.size(); i++) {


                int position = checked.keyAt(i);
                // Add slected if truee
                if (checked.valueAt(i))
                    selectedItems.add(adapter.getItem(position));

                prefsEditor.putFloat(POINT_LATITUDE_KEY + i, Float.parseFloat(values.get(position).getLat()));
                prefsEditor.putFloat(POINT_LONGITUDE_KEY + i, Float.parseFloat(values.get(position).getLng()));


            }
            prefsEditor.commit();

            String[] outputStrArr = new String[selectedItems.size()];



            for (int i = 0; i < selectedItems.size(); i++) {
                outputStrArr[i] = String.valueOf(selectedItems.get(i));
            }


            Bundle b = new Bundle();


            Location location = new Location("POINT_LOCATION");

           for (int i = 0; i < checked.size(); i++) {

                location.setLatitude(prefs.getFloat(POINT_LATITUDE_KEY + i, 0));
                location.setLongitude(prefs.getFloat(POINT_LONGITUDE_KEY + i, 0));

                double latitude = prefs.getFloat(POINT_LATITUDE_KEY + i, 0);
                double longitude = prefs.getFloat(POINT_LONGITUDE_KEY + i, 0);
                prefsEditor.commit();

                b.putDouble("Latitude" + i, latitude );
                b.putDouble("Longitude"  + i, longitude);



                }

            int banyakPilih = checked.size();
            b.putInt("banyakPilih", banyakPilih);

            Intent intent = new Intent(getApplicationContext(),
                    HasilPilihanActivity.class);

            // Create a bundle object
            b.putStringArray("selectedItems", outputStrArr);

            // Add the bundle to the intent.
            intent.putExtras(b);

            // start the ResultActivity
            startActivity(intent);
        }

I save my Prefernces in DATA_COOR.xml file name, it save my string, but i got another file that save my preference with file name DATA_Preferences in my Explorer. Some body can give me solution? Thanks before..

1条回答
ら.Afraid
2楼-- · 2019-09-06 03:22

Define private SharedPreferences mediaPrefs = null;

put this on your constructor mediaPrefs = this.getSharedPreferences("Testing", 1);

put below method to the source:

public void storeStateString(String prefsKeys, Float prefsValue) {
        SharedPreferences.Editor prefEditor = mediaPrefs.edit();
        prefEditor.putFloat(prefsKeys, prefsValue);
        prefEditor.commit();
    }

Use this method to store the state like:

storeStateString("POINT_LATITUDE_KEY"+i,Float.parseFloat(values.get(position).getLat()));

and now you can get this preference through:

Float finalValue = mediaPrefs.getFloat("POINT_LATITUDE_KEY1","2.0l");

where 2.0l is defalut value if mediaPrefs is null;

Let me know if any issues regarding that.

查看更多
登录 后发表回答