如何用教师工作的学校列表填充ListView

时间:2015-09-27 01:30:39

标签: android android-listview android-cardview

大家好,我有疑问。如何填充ListView,我有一位在几所大学学校工作的老师。

例如:

** Jonh Braz老师在哈佛大学,耶鲁大学和华盛顿大学工作。

**教师比尔汉斯在哈佛大学和耶鲁大学学习。

我需要为老师工作的每所学校出示一张卡片。

在我的例子中,我需要在应用程序3卡上显示John教师(因为John在3所学校工作)和2张教师Bill的卡片(因为Bill工作在2 scholl)

跟随我的班级老师

public class Teacher{

   private int id;
   private String nome;
   private private List<School> schoolList;

    */ gets and sets here /*
}

跟随我的班级学校

 public class School{

   private int id;
   private String schoolName;

    */ gets and sets here /*
}

现在我需要用适配器制作一个带有适配器的listView,以便老师工作的学校显示1张卡片。

我不知道它是怎么做的

按照我的类适配器

 public class TeacherAdapter extends RecyclerView.Adapter<TeacherAdapter.ViewHolder> {

    private List<Teacher> mTeacherList;

    public TeacherAdapter(List<Teacher> data) {
    mTeacherList = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView =             LayoutInflater.from(parent.getContext()).inflate(R.layout.teacher_item, parent,        false);
         return new ViewHolder(itemView);
     }

     @Override
     public void onBindViewHolder(ViewHolder holder, int position) {
     Teacher teacher = mTeacherList.get(position);

     for (int i = 0; i < teacher.getSchoolList().size(); i++) {
         holder.textViewTeacherNome.setText(teacher.getNome());
             holder.textViewSchoolName.setText(teacher.getConsultorioList().get(i).getName())   ;
       }
   }

     @Override
     public int getItemCount() {
     return mTeacherList.size();
  }

  public class ViewHolder extends RecyclerView.ViewHolder {

      public TextView textViewTeeacherNome;
      public TextView textViewSchoolName;


      public ViewHolder(View itemView) {
        super(itemView);

        textViewTeacherNome = (TextView)      itemView.findViewById(R.id.text_view_name_teacher);
           textViewSchoolName = (TextView)      itemView.findViewById(R.id.text_view_name_school);
       }
   }

}

这个课程不起作用,老师只显示一张卡片,我需要在学校给我看一张卡片的卡片。

1 个答案:

答案 0 :(得分:0)

ListView旨在显示自定义对象列表,每个对象代表一个对应的视图项。 在您的情况下,我建议您创建一个包含Teacher和School对象的新类。

public class TeacherSchoolWrapper {
    private Teacher mTeacher;
    private School mSchool;

    public TeacherSchoolWrapper(Teacher teacher, School school) {
        mTeacher = teacher;
        mSchool = school;
    }

    /* Getters and setters go here...*/
}

初始化数据时,请创建TeacherSchoolWrapper的ArrayList,然后填写教师和学校的数据。

public ArrayList<TeacherSchoolWrapper> initialize(List<Teacher> teachers) {
    ArrayList<TeacherSchoolWrapper> wrappers = new ArrayList<>();
    for (Teacher teacher : teachers) {
        // Create wrappers which contain both teacher and school
        for (School school : teacher.getSchoolList()) {
            wrappers.add(new TeacherSchoolWrapper(teacher, school));
        }
    }
    return wrappers;
}

创建解释数据的适配器类。

public class TeacherAdapter extends ArrayAdapter<TeacherSchoolWrapper> {
    public TeacherAdapter(ArrayList<TeacherSchoolWrapper> wrappers) {
        super(this, 0, wrappers);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, null);
        }
        TeacherSchoolWrapper wrapper = getItem(position);
        TextView textView1 = (TextView) convertView.findViewById(android.R.id.text1);
        TextView textView2 = (TextView) convertView.findViewById(android.R.id.text2);
        textView1.setText(wrapper.getTeacher().getName());
        textView2.setText(wrapper.getSchool().getName());
        return convertView;
    }
}

希望这可以帮到你。