如何从click方法的适配器类中的string.xml中获取字符串 [英] how to get string from string.xml in adapter class on click method

查看:64
本文介绍了如何从click方法的适配器类中的string.xml中获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我实现下面的单击 if(postion == 1)中的代码时,我想在on click方法的视图持有器中的适配器类中获取字符串,我的应用程序崩溃.

I want to get string in my adapter class on view holder in on click method my app crash when i implement the code write below in on click if(postion==1).

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

    private Context context;
    String SongURL;
    private String[] data;
    public Adapter(String[] data){
        this.data = data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_list_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        String title = data[position];
        holder.text.setText(title);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView img;
        TextView text;

        public ViewHolder(final View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.image1);
            text = (TextView) itemView.findViewById(R.id.text1);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int postion =getAdapterPosition();
                    if (postion==0){
                        String url = "http://sound41.songsbling.link/2016/english-songs/loud-rihanna/12%20-%20Love%20The%20Way%20You%20Lie%20Part%20I%20Bonus%20From%20Us%20%20-%20Loud%20-%20[SongsPK.city].mp3";
                        Intent intent = new Intent(v.getContext(),player.class);
                        intent.putExtra("url",url);
                        v.getContext().startActivity(intent);
                    }
                    if (postion==1){

                        // *This code crashes the app*
                        String url=Resources.getSystem().getString(R.string.song);

                    Intent intent = new Intent(v.getContext(),player.class);
                    intent.putExtra("url",url);
                    v.getContext().startActivity(intent);
                }
            }
        });

        }
    }
}

推荐答案

如果您查看

If you look at the docs you'll see that Resources.getSystem().getStrings will provide you with system wide strings, not the ones in your application (strings.xml).

替换下面的行

String url = Resources.getSystem().getString(R.string.song);

具有以下内容.

String url = v.getContext().getResources().getString(R.string.song);

这篇关于如何从click方法的适配器类中的string.xml中获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆