我的问题如下。我希望能够将文本文件读入数组适配器。我有一个 TextView 和 3 个 RadioButtons。到目前为止,我已经设法传递了一个可用于更新 TextView 的字符串数组。我有一个数组列表,如下所示
ArrayList<info> list = new ArrayList<info>();
for(int i=1; i <= 3; i++)
{
Reader reader = new ResultsReader("C:/Users/ALEXDEV/workspace/Questions/src/quiz"+i+".txt");
reader.read();
String str = ((ResultsReader)reader).getInput();
String data[] = str.split("<.>");
info = q new info();
q.question = data[0];
q.choice1 = data[2];
q.choice2 = data[3];
q.choice3 = data[4];
list.add(q);
}
for(Question qs: list) {
System.out.println("Q: "+qs.question);
System.out.println("Q: "+qs.choice1);
System.out.println("Q: "+qs.choice2);
System.out.println("Q: "+qs.choice3);
}
-
Q:问题
Q:回答一
Q:回答二
Q:回答三
所以我想做的是将arraylist传递给arrayadapter,这样我就可以使用getView方法返回各种结果
到目前为止,我已经尝试将作为数组列表的列表传递给 MySimpleArrayAdapter 适配器 = new MySimpleArrayAdapter(this, list);
公共类 CustomAdapter 扩展 ArrayAdapter {
private ArrayList<Question> list;
private Context context;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<Question> list) {
super(context, textViewResourceId,list);
this.context = context;
this.list = list;
// TODO Auto-generated constructor stub
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.basic, null);
}
Question element = list.get(position);
if (element!= null) {
// My layout has only one TextView
TextView itemView = (TextView) view.findViewById(R.id.questionText);
if (itemView != null) {
// do whatever you want with your string and long
itemView.setText(String.format("%s %d", element.question, element.answer));
}
}
return view;
}