嗨,我正在尝试从保存在xml文件中的多个字符串数组中获取信息,以在回收者视图中的卡片视图中输出该信息。我以为我可以通过适配器文件访问回收站视图的阵列,但是我不确定,因为这是我的新手。
我的xml文件称为events.xml,位于res / values文件夹中。
我的适配器代码是这样的:
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;
}
}
您可以在构造函数中获取String数组:
public EventCalenderAdapter(Context context){
title= context.getResources().getStringArray(R.array.titleArray);
//... get the rest of your arrays
}
然后,当实例化适配器(new EventCalenderAdapter(YourActivity);
)时,将加载阵列。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句