如何从字符串资源填充RecyclerView



我得到的是字符串id,而不是recyclerview中的字符串值。

下面是我的代码,我不知道哪里出错了。

public class Albert_Einstein_Fragment extends Fragment {
private String b;
String[] dataArray ={String.valueOf(R.array.albert_array)};
//...
private class QuoteAdapter extends RecyclerView.Adapter<QuoteHolder>{
    private String[] dataSource;
    public QuoteAdapter(String[] dataArgs){
        dataSource = dataArgs;
    }
    @Override
    public QuoteHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater
                .inflate(R.layout.list_quotes, parent, false);
        return new QuoteHolder(view);
    }

    @Override
    public void onBindViewHolder(QuoteHolder holder, int position) {
        holder.mTaskNameTextView.setText(dataSource[position]);
    }

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

下面是我的数组字符串资源代码的一部分。。。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="albert_array">
    <item>Let every man be respected as an individual and no man idolized.   </item>
    <item>God does not play dice. </item>
    <item>Great spirits have always encountered violent opposition from mediocre minds.</item>
    <item>Only a life lived for others is a life worthwhile.</item>
    <item>We shall require a substantially new manner of thinking if mankind is to survive.</item>
    <item>I used to go away for weeks in a state of confusion.</item>
</string-array>
</resources>

下面是我的代码的输出

2131427328

而不是我的数组字符串资源中的字符串列表

要检索String数组资源,需要使用Resources#getStringArray()方法。您可以使用getResources()方法访问应用程序的Resources,该方法必须在Context上调用;在您的情况下,Activity就足够了。

dataArray的声明行删除初始化:

private String[] dataArray;

在初始化activity之后,在onCreateView()方法中添加以下内容;

dataArray = activity.getResources().getStringArray(R.array.albert_array);