如何在图像Recyclerview中使用sharedPreference

时间:2018-06-03 15:47:39

标签: android android-recyclerview android-imageview android-alertdialog

我有两个活动,Treasure和MapsActivity 这是宝藏:

enter image description here

然后当我点击图片时,它会给出:

enter image description here

当我的答案是正确的时,它给出了这个:

enter image description here

但是当我去其他图像并在那里做活动,或者回到其他活动时,它会回到image1。 我尝试使用像这样的sharedPreferences:

.setNegativeButton("Ketemu!",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                SharedPreferences preferences = getSharedPreferences("input_Gallery", MODE_PRIVATE);
                                final String input = etUserInput.getText().toString();
                                if(input.equals("harmonia")){
                                    SharedPreferences.Editor editor = preferences.edit();
                                    editor.putString("input", input);
                                    editor.apply();
                                    adapter.changeImage(position);
                                    Toast.makeText(getApplicationContext(),"Selamat!, silahkan pilih Treasure",Toast.LENGTH_SHORT).show();
//                                    hasil.setText(etUserInput.getText());
//                                    selamat();
                                }else{
                                    Toast.makeText(getApplicationContext(),"Maaf, Jawaban salah. Silahkan pergi ke peta",Toast.LENGTH_SHORT).show();
                                }

                            }
                        })
                .create()
                .show();

然后在onCreate方法上,我用这个:

SharedPreferences preferences = getSharedPreferences("input_Gallery", MODE_PRIVATE);
        inputFinal1 = preferences.getString("input", "Key not correct");

但我不知道接下来该做什么,因为我想要显示它应该显示的图像,而不是要显示的文本或字符串。 任何帮助都会很感激。谢谢..

changeImage来自MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

    private LayoutInflater inflater;
    List<Information> data = Collections.emptyList();
    private Context context;
    private ClickListener clickListener;

    public MyAdapter(Context context, List<Information> data){
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        TextView subtitle;
        ImageView icon, iconth;
        public ViewHolder(View itemView){
            super(itemView);
//            title = itemView.findViewById(R.id.list_title);
//            icon = itemView.findViewById(R.id.list_gambar);
            itemView.setOnClickListener(this);
            iconth = (ImageView) itemView.findViewById(R.id.icon_th);
            iconth.setOnClickListener(this);
//            icon.setOnClickListener(this);
            subtitle = itemView.findViewById(R.id.list_subtitle); // disini juga bisa
//            Intent intent = new Intent(LoginActivity.this, slide.class);
//            intent.putExtra("treasure", subtitles[i]);
//            LoginActivity.this.startActivity(intent);

        }

        @Override
        public void onClick(View v) {
//            context.startActivity(new Intent(context, MapsActivity.class));

            if(clickListener != null){
                clickListener.itemClicked(v, getAdapterPosition());
            }else{
                Toast.makeText(v.getContext(),"Null", Toast.LENGTH_SHORT).show();
            }
            Toast.makeText(v.getContext(), "Treasure " + getAdapterPosition() +" di klik", Toast.LENGTH_SHORT).show();
        }
    }

    public void setClickListener(ClickListener clickListener){
        this.clickListener = clickListener;
    }

    public interface ClickListener{
        public void itemClicked(View view, int position);
    }

    public void delete(int position){
        data.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = inflater.inflate(R.layout.custom_row, parent, false);
        //...
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    public void changeImage(int position) {

        data.get(position).setImageChanged(true);
        notifyItemChanged(position);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Information current = data.get(position);
//        holder.title.setText(current.title);
        holder.subtitle.setText(current.subtitle);
        holder.iconth.setImageResource(current.iconId);
        if(data.get(position).isImageChanged()){
            holder.iconth.setImageResource(R.drawable.th_ok);
        }else{
            holder.iconth.setImageResource(current.iconId);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

0 个答案:

没有答案