【问题标题】:pass a text file to an arrayadapter将文本文件传递给 arrayadapter
【发布时间】:2012-06-10 13:37:20
【问题描述】:

我的问题如下。我希望能够将文本文件读入数组适配器。我有一个 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);   
    }

-

问:问题
问:回答一个
问:回答二
问:回答三

所以我要做的就是将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;

}

【问题讨论】:

    标签: android arraylist android-arrayadapter


    【解决方案1】:

    您需要创建一个自定义适配器来提供列表视图。

    您可以创建自定义 ArrayAdapter(例如 here)或 BaseAdapter(例如 here)。

    在任何一种情况下,您都将覆盖适配器的 getView 方法,以将数据处理到布局中的各种小部件中。

    编辑

    如果问题实际上是“我如何调用适配器?”那么:

    private CustomAdapter my_adapter;
    
    my_adapter = new CustomAdapter(this, R.layout.row, list);
    setListAdapter(my_adapter);
    

    然后按照上面的链接之一创建您的CustomAdapter

    【讨论】:

    • 欢迎来到 SO!由于您是新手,您可能需要查看此link。祝你好运!
    • 如果我对此问题还有其他疑问并想添加评论,我是否需要取消勾选我的答案,以便人们知道这是一个需要回答的问题
    • 这真的取决于。我们试图在这里保持“每个帖子一个问题”的事情......让其他人更容易在他们的具体问题上找到帮助。您询问了将文本文件/数组列表传递给适配器的问题。现在,如果您对为所述阵列实施适配器有疑问,IMO 这将是一个新问题。如果您认为给出的解决方案可行,但存在一些复杂性使其无法正常工作,那么请务必不接受该答案并编辑您的问题。不要在评论中提出更多问题,除非是“该代码到底去哪儿了?”之类的后续问题。
    • 谢谢,我会记住将我遇到的每个特定问题分开,而不仅仅是一个大的一般性问题。
    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多