Reputation: 11
I'm developing the settings of my App using PreferenceActivity and PreferenceFragment, I'm new in this and I found a problem with one of my custom preferences.
The ImagePreference is a Preference that shows the user profile image and the problem is that If I choose an image from my Gallery and then check/uncheck one of the ChecboxPreferences, the image changes to the first one as I've never choose a new image.
SettingsFragment
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
imagePreference = (ImagePreference) findPreference("cambiar_foto");
imagePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(GaleryIntent, 0);
return true;
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if ( resultCode == Activity.RESULT_OK && null != data) {
...
mProfileImage = (ImageView) getActivity().findViewById(R.id.iv_ic_foto);
mProfileImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Toast.makeText(getActivity(), picturePath, Toast.LENGTH_SHORT).show();
}
}
}
}
preferences.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<...>
<PreferenceCategory
android:title="Cambiar o agregar foto">
<com.zonaapp.taxis.demo.preferences.ImagePreference
android:key="cambiar_foto"/>
<PreferenceCategory
android:title="Cobertura de información">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="Mi ciudad"/>
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="Mi país"/>
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="Todos"/>
</PreferenceCategory>
<...>
</PreferenceScreen>
Custom Preference Class:
public class ImagePreference extends Preference {
private Context mContext;
public ImagePreference(Context context) {
super(context);
mContext = context;
}
public ImagePreference(Context context, AttributeSet attributeSet){
super(context, attributeSet);
mContext = context;
}
public ImagePreference(Context context, AttributeSet attributeSet, int defStyle){
super(context, attributeSet, defStyle);
mContext = context;
}
public View getView(View convertView, ViewGroup parent){
View row = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.preference_screen_cambiar_foto, parent, false);
}
ImageView imageView = (ImageView) row.findViewById(R.id.iv_ic_foto);
Picasso.with(mContext).load(ParseUser.getCurrentUser().getParseFile("profilePic").getUrl()).into(imageView);
return row;
}
}
Upvotes: 1
Views: 252
Reputation: 392
You will need to Save your Preference's State . Apply the procedure as shown in the Official Docs.
http://developer.android.com/guide/topics/ui/settings.html#CustomSaveState
and Saving and restoring the Preference's state
Upvotes: 1