【问题标题】:Show arraylist in android frontend在android前端显示arraylist
【发布时间】:2014-10-22 02:09:56
【问题描述】:

我一直在寻找将 Arraylist 放到 android 中的 ListView 上的方法,但一直没能做到。这是我使用的程序,但它似乎只适用于 String 数组而不适用于 List 元素。

List<Students> studentList = new ArrayList<Students>();
ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
                this,
                android.R.layout.simple_list_item_single_choice,
                studentList);
listView.setAdapter(arrayAdapter);

我需要找到一个解决方案,我可以将这个 arraylist 放到 listview 中,而不必将它转换为一个 STring 数组。这样的事情有可能吗? 提前致谢。 :-)

【问题讨论】:

  • 你必须制作一个自定义适配器
  • 使用自定义适配器类

标签: java android list listview arraylist


【解决方案1】:

您必须重写适配器的 getView 方法才能实现所需的功能:

ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
        this,
        android.R.layout.simple_list_item_single_choice,
        studentList) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Let the system create and re-use the views
        View view = super.getView(position, convertView, parent);

        // Get the default android layout's TextView
        TextView textView = (TextView) view.findViewById(android.R.id.text1);

        // Get the students info (name or w/e)
        Students student = getItem(position);
        String studentName = student.getName();
        textView.setText(studentName);

        return view;
    }
};

【讨论】:

    猜你喜欢
    • 2017-08-15
    • 2017-12-30
    • 2018-11-26
    • 2019-01-03
    • 2013-01-23
    • 2011-08-16
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多