How to get data from xml file into multiple string arrays in android?
Hey. I am trying to get information from multiple arrays of strings stored in an XML file for output as a map in a recycler view. I think I am accessing the arrays through the adapter file to view the recycler, however I am not sure as I am new to this.
My xml file is called event.xml and is located in res / values folder.
My adapter code is like 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;
}
}
+3
source to share
1 answer
You can get string arrays in the constructor:
public EventCalenderAdapter(Context context){
title= context.getResources().getStringArray(R.array.titleArray);
//... get the rest of your arrays
}
Then when you initiate your adapter ( new EventCalenderAdapter(YourActivity);
) your arrays will be loaded.
+1
source to share