我可以将哪些其他适配器用于ListView?

时间:2016-02-19 00:43:20

标签: android

我目前正在为ListView使用SimpleAdapter。我只想知道是否有任何可用于从JSON对象显示的适配器。

ListAdapter adapter = new SimpleAdapter(CheckAttendanceActivity.this, studentList, R.layout.list_students, new String[]{Tags.getIDNO(), Tags.getREMARK(), Tags.getFULLNAME()}, new int[]{R.id.idno, R.id.remark, R.id.fullname}) {
                        @Override
                        public View getView(int position, View convertView, ViewGroup parent) {
                            if (convertView == null) {
                                convertView = LayoutInflater.from(CheckAttendanceActivity.this).inflate(R.layout.list_students, parent, false);
                            }

                            TextView fullname = (TextView) convertView.findViewById(R.id.fullname);
                            TextView idno = (TextView) convertView.findViewById(R.id.idno);
                            TextView remark = (TextView) convertView.findViewById(R.id.remark);

                            HashMap<String, Object> obj = (HashMap<String, Object>) getItem(position);
                            if(obj.get("remark").equals("present")) {
                                convertView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPresent));
                                fullname.setTextColor(Color.WHITE);
                            }
                            else if(obj.get("remark").equals("late")) {
                                convertView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorLate));
                                fullname.setTextColor(Color.WHITE);
                            }
                            else {
                                convertView.setBackgroundColor(Color.WHITE);
                                fullname.setTextColor(Color.BLACK);
                            }

                            fullname.setText(obj.get("fullname").toString());
                            idno.setText(obj.get("idno").toString());
                            remark.setText(obj.get("remark").toString());


                            return convertView;
                        }
                    };

studentList为ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>(),在其中我将一些HashMap<String, String> map = new HashMap<String, String>()样本放入地图中

map.put("idno", "01");
map.put("remark", "absent");
map.put("fullname", "John");

studentList.add(map);

我可以用于此的任何其他适配器吗?有样品吗?

1 个答案:

答案 0 :(得分:3)

您可以像这样扩展BaseAdapter;

public class MyAdapter extends BaseAdapter {

        private Context context;
        private List<Student> students;

        public MyAdapter(List<Student> students, Context context) {
            this.students = students;
            this.context = context;
        }

        @Override
        public int getCount() {
            return students.size();
        }

        @Override
        public Object getItem(int i) {
            return students.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            //Similar to SimpleAdapter's getView()
            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.list_students, viewGroup, false);
            }

            //Bind views
            TextView fullname = (TextView) view.findViewById(R.id.fullname);
            TextView idno = (TextView) view.findViewById(R.id.idno);
            TextView remark = (TextView) view.findViewById(R.id.remark);

            Student student = (Student) getItem(i);

            fullname.setText(student.getName());
            idno.setText(String.valueOf(student.getIdno()));
            remark.setText(student.getRemark());

            return view;
        }
    }

学生模型

public class Student {

    private String name;
    private int idno;
    private String remark;

    public Student(String name, int idno, String remark) {
        this.name = name;
        this.idno = idno;
        this.remark = remark;
    }

    public String getName() {
        return name;
    }

    public int getIdno() {
        return idno;
    }

    public String getRemark() {
        return remark;
    }
}

在活动中设置适配器

public void setupList(ListView listView) {

    List<Student> students = new ArrayList<>();
    students.add(new Student("aaa", 12, "bad"));
    students.add(new Student("bbb", 14, "good"));

    MyAdapter adapter = new MyAdapter(students, this);
    listView.setAdapter(adapter);
}

也就是说,您应该考虑使用RecyclerView,因为它在各个方面都优于ListView

相关问题