Android - getSharedPreferences无法解析

时间:2017-09-23 15:35:42

标签: javascript android

你好我有getSharedPreferences无法解决,我搜索了很多答案,但任何事情都解决了这个问题。 如何在onclick iside ViewHolder中实现getSharedPreferences?

MyHolder.class

 public class MyHolder extends RecyclerView.ViewHolder {

    TextView nametxt;
    ImageView img;
    TextView url;
    TextView channel;
    Context c;


    public MyHolder(View itemView) {
        super(itemView);


        nametxt= (TextView) itemView.findViewById(R.id.nameTxt);
        img= (ImageView) itemView.findViewById(R.id.movieImage);
        url= (TextView) itemView.findViewById(R.id.urltxt);
        channel= (TextView) itemView.findViewById(R.id.channel);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), VideoPlayerActivity.class);
                SharedPreferences prefs = getSharedPreferences("My_Prefs", MODE_PRIVATE);
                String user = prefs.getString("user", "");

                String urls= url.getText().toString()+user+user+channel.getText().toString();
                intent.putExtra("urls", urls);


                v.getContext().startActivity(intent);

            }
        });



    }




}

3 个答案:

答案 0 :(得分:1)

更改: -

SharedPreferences prefs = v.getContext().getSharedPreferences("My_Prefs", MODE_PRIVATE);

到此: -

{{1}}

答案 1 :(得分:1)

而不是

SharedPreferences prefs = getSharedPreferences("My_Prefs", MODE_PRIVATE);

使用context尝试此操作,您可以从context

视图中获取v
SharedPreferences prefs = v.getContext().getSharedPreferences("My_Prefs", MODE_PRIVATE);

答案 2 :(得分:0)

您可以从上下文访问共享偏好设置,并且您可以访问上下文v.getContext