首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在BindViewHolder中检索共享首选项

在BindViewHolder中检索共享首选项

提问于 2018-06-06 07:47:08
回答 1关注 0查看 198

我在MapsActivity中写了sharedPreference,它接受一个答案,如果答案正确,回收视图中的图像将会改变,但是onBindViewHolder中的getSharedPreference是红色的,而且,我试图得到

代码语言:txt
复制
Resources res = getResources();
            Drawable newimages = res.getDrawable(R.drawable.TH_logo_solo_ok);

getResources();也是红色的,这在里面适用吗?onBindViewHolder?或者我的方法是错的。

这只是代码的一部分。MapsActivity

代码语言:txt
复制
.setPositiveButton("Ketemu!",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int id) {
                                        SharedPreferences preferences = getSharedPreferences("idTreasure", MODE_PRIVATE);
                                        hasil.setText(userInput.getText());
                                        if(hasil.getText().toString().equals("harmonia")){
                                            SharedPreferences.Editor editor = preferences.edit();
                                            String idGambar = hasil.getText().toString();
                                            editor.putString("idGambar",idGambar);
                                            editor.apply();
                                            selamat();
                                        }else{
                                            salah();
                                        }
                                    }
                                })

这是我的onBindViewHolder在我的适配器上

代码语言:txt
复制
@Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Information current = data.get(position);
//        holder.title.setText(current.title);
        holder.iconth.setImageResource(current.iconth);
        holder.subtitle.setText(current.subtitle);
    }

基本上,我想在下面的代码中实现所有这些代码onBindViewHolder因为那是我唯一知道的改变回收视图中图像的地方,

代码语言:txt
复制
SharedPreferences preferences = getSharedPreferences("idTreasure", MODE_PRIVATE);
    String ImagePrefs = preferences.getString("idGambar", "Key not correct");
    tim = (TextView) findViewById(R.id.tim);
    tim.setText(ImagePrefs);
    if(ImagePrefs.getText().toString().equals("harmonia")){
        Resources res = getResources();
        Drawable newimages = res.getDrawable(R.drawable.TH_logo_solo_ok);
        holder.iconth.setImageResource(current.newimages);
    }

我该怎么做,或者用另一种方式?

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档