For this opportunity I need to use non-default (i.e. custom) sharedpreference data. How can I access this custom sharedpreference data in a custom preference layout? In this case I cannot use getDefaultSharedPreferences(context);
I built a custom listpreference that allows adding new elements to it. I had to work with Android 4. For this question that is not very important.
The simple MainActivity is just showing the preference list with only 1 custom preference layout.
public class MainActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager prefMgr = getPreferenceManager();
prefMgr.setSharedPreferencesName("my_preferences");
prefMgr.setSharedPreferencesMode(MODE_PRIVATE);
try {
addPreferencesFromResource( R.xml.preferences);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The res/xml/preferences.xml file contains:
<nl.xyz.listpreferenceexpandable.ListPreferenceExpandable
android:key="custom3"
android:title="CustomList3"
android:dialogTitle="Add custom item"
android:entries="@array/customdata1"
android:entryValues="@array/customdata1"/>
The custom ListPreference code is shown below. Question is: how can I access the custom shared preference data with the name "my_preferences" ? When I call just a getPreferenceManager() then I get null.
public class ListPreferenceExpandable extends ListPreference {
CustomListPreferenceAdapter customListPreferenceAdapter = null;
Context mContext;
private LayoutInflater mInflater;
List<String> entries;
ArrayList<RadioButton> rButtonList;
SharedPreferences prefs;
SharedPreferences.Editor editor;
String current;
String keyDefault;
String keyData;
public ListPreferenceExpandable(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mInflater = LayoutInflater.from(context);
rButtonList = new ArrayList<RadioButton>();
// THIS IS WRONG:
prefs = PreferenceManager.getDefaultSharedPreferences(context);
// QUESTION: How can I access SharedPreference "my_preferences"
editor = prefs.edit();
keyDefault = getKey();
keyData = getKey() + "_data"; // will hold the dynamically data
String prefDefault = getPersistedString( getKey() );
current = prefs.getString( keyDefault, prefDefault);
setSummary( current);
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
getData();
customListPreferenceAdapter = new CustomListPreferenceAdapter(mContext);
builder.setAdapter(customListPreferenceAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println( "Clicked ... on " + which);
}
});
builder.setNeutralButton( "Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
inputDialog( "Add new entry");
}
});
}
private void getData() {
String data = prefs.getString( keyData, "");
if( data.isEmpty()) {
entries = new ArrayList<>();
CharSequence[] loadedFromTheResource = getEntries();
if( loadedFromTheResource == null || loadedFromTheResource.length < 1) {
loadedFromTheResource = getEntryValues();
}
if( loadedFromTheResource != null && loadedFromTheResource.length > 0) {
for (CharSequence cs : loadedFromTheResource) {
entries.add(cs.toString());
}
}
} else {
String[] loaded = data.split("\\s*#\\s*");
if( loaded != null && loaded.length > 0) {
entries = Arrays.asList( loaded);
entries = new ArrayList<>(entries);
}
}
if( entries.isEmpty()) {
entries.add(current);
}
}
private void saveData() {
SharedPreferences.Editor editor = prefs.edit();
editor.putString( keyData, TextUtils.join("#", entries));
editor.apply();
}
void inputDialog( String title) {
Builder builder = new Builder( mContext); // this
builder.setTitle(title);
final EditText input = new EditText( mContext); // this
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newEntry = input.getText().toString();
entries.add( newEntry);
customListPreferenceAdapter.notifyDataSetChanged();
saveData();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
static class ViewHolder {
protected TextView nameT = null;
protected TextView formulaT = null;
}
private class CustomListPreferenceAdapter extends BaseAdapter {
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;
public CustomListPreferenceAdapter(Context context) { }
public int getCount() {
return entries.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder;
if(convertView == null) {
row = mInflater.inflate(R.layout.custom_list_preference_row, parent, false);
holder = new CustomHolder( row, position);
row.setTag(holder);
} else {
holder = (CustomHolder) row.getTag();
}
holder.row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String current = entries.get(position).toString();
editor.putString(keyDefault, current).apply();
setSummary(current);
Dialog mDialog = getDialog();
mDialog.dismiss();
}
});
String currentSelection = prefs.getString( keyDefault, "");
if( entries.get(position).equals( currentSelection)) {
holder.rButton.setChecked(true);
} else {
holder.rButton.setChecked(false);
}
holder.text.setText( entries.get(position));
return row;
}
class CustomHolder {
private TextView text = null;
private RadioButton rButton = null;
private View row = null;
CustomHolder( View row, int position1) {
text = (TextView)row.findViewById(R.id.custom_list_view_row_text_view);
rButton = (RadioButton)row.findViewById(R.id.custom_list_view_row_radio_button);
this.row = row;
}
public RadioButton getRadioButton() {
return rButton;
}
}
}
}
In arrays.xml "@array/customdata1" is just:
<string-array name="customdata1">
<item name="One">One</item>
<item name="Two">Two</item>
</string-array>
The row is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_list_view_row_table_row"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dip"
android:paddingTop="8dip"
android:paddingLeft="10dip"
android:paddingRight="10dip">
<TextView
android:id="@+id/custom_list_view_row_text_view"
android:textSize="22sp"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<RadioButton
android:checked="false"
android:layout_width="40dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:id="@+id/custom_list_view_row_radio_button"/>
</LinearLayout>
@X3Btel, thank you for showing me the way.
The custom ListPreferenceExpandable should be re-used many times. The old app used a separate SharedPreference for each different target group.
So how could I communicate the name of the SharedPreference to the custom ListPreference?
The easiest way was to create a separate preference.xml file for each targetgroup. In each preference_targetgroup_n.xml I put the name of the sharedPreference of that target group.
preference_targetgropu_n.xml:
<nl.xyz.listpreferenceexpandable.ListPreferenceExpandable
android:key="custom3"
android:title="CustomList3"
android:dialogTitle="Add custom item"
android:entries="@array/customdata1"
**** sharedPreference="sharedpreferences_of_targetgroup_n" ****
android:entryValues="@array/customdata1"/>
To make my ListPreference re-usable in this case I put in the custom preference:
String sharedPreference = attrs.getAttributeValue(null, "sharedPreference");
if( sharedPreference == null || sharedPreference.isEmpty()) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
} else {
prefs = context.getSharedPreferences(sharedPreference, MODE_PRIVATE);
}