保存DialogFragment可保存首选项中的可绘制图像

时间:2016-12-21 09:40:38

标签: android android-dialogfragment

如果用户已加载图片之前,我正在尝试加载图片。我试图在 onCreate

中执行此操作

使用 DialogFragment 来实现此功能,因为我只想从 应用中选择图片。

这是我的 WithNamesFragment Class

public class WithNamesFragment extends DialogFragment {
    final static ArrayList<HashMap<String, ?>> imagesWithNames = new ArrayList<>();

    static {
        HashMap<String, Object> row = new HashMap<>();
        row.put("imageID", R.drawable.aquarius);
        row.put("name", "Aquarius");
        imagesWithNames.add(row);
        row = new HashMap<>();
        row.put("imageID", R.drawable.virgo);
        row.put("name", "Virgo");
        imagesWithNames.add(row);

    }

    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SimpleAdapter adapter = new SimpleAdapter(getContext(), imagesWithNames, R.layout.lib_dialog_image,
                new String[]{"name", "imageID"}, new int[]{R.id.text1, R.id.image1});
        return new AlertDialog.Builder(getContext()).setAdapter(adapter,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ((MainActivity) getContext()).OnSelected(WithNamesFragment.this.getClass(), (int) ((HashMap<String, Object>) adapter.getItem(i)).get("imageID"));
                    }
                }).setCancelable(true).setTitle("PICK YOUR AVATAR").create();
    }
}
主要活动

public void OnSelected(Class<?> clazz, int i) {
        Toast toast = Toast.makeText(this, "Image Selected", Toast.LENGTH_SHORT);
        //Toast toast = Toast.makeText(this, "Class \"" + clazz.getSimpleName() + "\" returns this image with id: " + getResources().getResourceName(i), Toast.LENGTH_SHORT);
        final TextView toastView = (TextView) toast.getView().findViewById(android.R.id.message);
        toastView.setCompoundDrawablesWithIntrinsicBounds(0, i, 0, 0);
        toast.show();
        Glide.with(this).load(i)
                .crossFade()
                .thumbnail(0.5f)
                .bitmapTransform(new CircleTransform(this))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(profimg);
        sp.edit().remove("dp").apply();
        i=this.i;
        sp.edit().putInt("dp",i);
    }
    void show() {
        new WithNamesFragment().show(getSupportFragmentManager(), PICKER_TAG);
    }
MainActivity Oncreate

profimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show();
            }
        });

 boolean path =sp.contains("dp");

if(path) {
           int value=sp.getInt("dp",i);
            Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
            /*Glide.with(this).load(value)
                    .crossFade()
                    .thumbnail(0.5f)
                    .bitmapTransform(new CircleTransform(this))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(profimg);*/
        }

这是show方法:

void show() {
        new WithNamesFragment().show(getSupportFragmentManager(), PICKER_TAG);
    }

早些时候我正在努力解决Casting错误,但现在我在Toast中一无所获。

1 个答案:

答案 0 :(得分:1)

替换以下行:

 sp.edit().putInt("dp",i).apply();

您错过了.apply()或.commit() 修改代码: 首先初始化SharedPreference。

    Oncreate(){

        final int value = sp.getInt("dp",0);

    if(value>0){
       imageView.setImageResource(value);
    }


}