1

I'm getting string id instead of string value in recyclerview.

Below is my code, I don't know where i'm getting it wrong.

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;
    }
}
}
}

Below is part of my array string resource code...

<?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>

Below is the output of my code

2131427328

Instead of list of strings in my array string resource

olajide
  • 972
  • 1
  • 13
  • 26
  • You're not retrieving your `String` array Resource correctly. Remove the initialization in the declaration line, and add the following in `onCreateView()`: `dataArray = activity.getResources().getIntArray(R.array.albert_array);`. – Mike M. Apr 14 '16 at 01:15
  • Wow! Works Perfectly! Thank you! thank you! But i changed `getIntArray()` to `getStringArray()` Thank you very much. – olajide Apr 14 '16 at 01:32
  • Oh, jeez, yeah, that was a typo. My bad. Glad you got it figured out. Cheers! – Mike M. Apr 14 '16 at 01:38
  • How do i connect with you sir? In case. – olajide Apr 14 '16 at 01:43
  • Wow! I which i could live like that. – olajide Apr 14 '16 at 02:14

1 Answers1

2

To retrieve a String array resource, you need to use the Resources#getStringArray() method. You can get access to your app's Resources with the getResources() method, which must be called on a Context; in your case, the Activity will suffice.

Remove the initialization from dataArray's declaration line:

private String[] dataArray;

And add the following in the onCreateView() method, after you've initialized activity;

dataArray = activity.getResources().getStringArray(R.array.albert_array);
Mike M.
  • 38,532
  • 8
  • 99
  • 95