如何从字符串资源填充RecyclerView

时间:2016-04-14 00:36:17

标签: java android

我在map中收到字符串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>

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

1 个答案:

答案 0 :(得分:2)

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

dataArray的声明行中删除初始化:

private String[] dataArray;

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

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