Tuesday, 13 August 2013

Android sort SharedPreference alphabetically

Android sort SharedPreference alphabetically

I just this code to save favourites, but I want the favourites to display
in alphabetical order:
SharedPreferences sp =
getActivity().getApplicationContext().getSharedPreferences("bookmarks",
Context.MODE_PRIVATE);
Map<String,?> keys = sp.getAll();
int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}
final SharedPreferences sp =
getActivity().getApplicationContext().getSharedPreferences("bookmarks",
Context.MODE_PRIVATE);
LayoutInflater vi = (LayoutInflater)
getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vx = vi.inflate(R.layout.single_bookmark, null);
final TextView text = (TextView) vx.findViewById(R.id.bookmark_text);
final ImageView starimage = (ImageView) vx.findViewById(R.id.star);
text.setText(values_array[1]);
// insert into main view
mBookmarkLayout.addView(vx, 0, new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
}
No matter what I do I cannot get it to work.
Thanks

No comments:

Post a Comment