3

Hi I am trying to get information from multiple string arrays held in a xml file to be output in a card view within a recycler view. I am thinking that I access the arrays through the adapter file for the recycler view, however I am not sure as I am new to this.

My xml file is called events.xml and is located in the res/values folder.

My adapter code is this:

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

String[] title;
String[] time_start;
String[] time_finish;
String[] date;

static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    TextView titleView;
    TextView auxView1;
    TextView auxView2;
    TextView auxView3;

    public ViewHolder(CardView card) {
        super(card);
        cardView = card;
        titleView = (TextView) card.findViewById(R.id.text1);
        auxView1 = (TextView) card.findViewById(R.id.text2);
        auxView2 = (TextView) card.findViewById(R.id.text3);
        auxView3 = (TextView) card.findViewById(R.id.text4);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
    CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
    viewHolder.titleView.setText(title[i]);
    viewHolder.auxView1.setText(time_start[i]);
    viewHolder.auxView2.setText(time_finish[i]);
    viewHolder.auxView3.setText(date[i]);

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



}
4
  • You can ge the string by String[] mydata = getResources.getStringArray(R.array.mystrings);. Then you can pass it to the constructor of the adapter class and use it there. Read developer.android.com/guide/topics/resources/… Commented Apr 25, 2015 at 8:09
  • @Raghunandan thank you for your comment however I get an error saying "Cannot resolve symbol 'getResources'" Commented Apr 25, 2015 at 8:13
  • you need to do that in a activity. Activity extends Context and getResources requires context Commented Apr 25, 2015 at 8:13
  • the other option is pass the context to the constructor of adapter class. Commented Apr 25, 2015 at 8:15

1 Answer 1

1

You can get your String arrays in constructor:

public EventCalenderAdapter(Context context){
   title= context.getResources().getStringArray(R.array.titleArray);
   //... get the rest of your arrays
}

Then when you instanciate your adapter (new EventCalenderAdapter(YourActivity); ), your arrays will be loaded.

Sign up to request clarification or add additional context in comments.

3 Comments

should I put the constructor in the adapter class or the fragment that calls the class, sorry I'm new to this.
my fragment is now not working saying that EventCalenderAdapter(context) cannot be applied
Try with EventCalenderAdapter(getActivity());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.