将ArrayAdapter与ArrayList一起使用

时间:2016-05-13 08:37:13

标签: android arraylist android-arrayadapter

我目前正在构建一个包含4个ListView的Android议程活动。基本上,我得到了由WebServiceCoordinator调用的onAgendaDataReady(ArrayList约会)方法。此方法对约会进行排序,这样就填充了4个新的ArrayList。代码如下:

@Override
public void onAgendaDataReady(ArrayList<Appointment> appointments) {
    //swipeContainer.setRefreshing(false);
    alert.hide();
    ArrayList<Appointment> appointmentsfuturs = new ArrayList<>();
    ArrayList<Appointment> appointmentspasses = new ArrayList<>();
    ArrayList<Appointment> appointmentsannules = new ArrayList<>();
    ArrayList<Appointment> appointmentsaconfimer = new ArrayList<>();

    for (Appointment a : appointments) {
        if (a.stateId == 2)
            appointmentsaconfimer.add(a);
        else if (a.stateId == 6)
            appointmentsannules.add(a);
        else if (a.stateId == 5)
            appointmentspasses.add(a);
        else if (a.stateId == 4)
            appointmentsfuturs.add(a);
    }

    ArrayAdapter<Appointment> adapterfuturs = new ArrayAdapter<>(AgendaActivity.this, android.R.layout.simple_list_item_1, appointmentsfuturs);
    ArrayAdapter<Appointment> adapterpasses = new ArrayAdapter<>(AgendaActivity.this, android.R.layout.simple_list_item_1, appointmentspasses);
    ArrayAdapter<Appointment> adapterannules = new ArrayAdapter<>(AgendaActivity.this, android.R.layout.simple_list_item_1, appointmentsannules);
    ArrayAdapter<Appointment> adapteraconfirmer = new ArrayAdapter<>(AgendaActivity.this, android.R.layout.simple_list_item_1, appointmentsaconfimer);

    rdvFutursListView.setAdapter(adapterfuturs);
    rdvPassesListView.setAdapter(adapterpasses);
    rdvAnnulesListView.setAdapter(adapterannules);
    rdvAConfirmerListView.setAdapter(adapteraconfirmer);

    adapterfuturs.notifyDataSetChanged();
    adapterpasses.notifyDataSetChanged();
    adapterannules.notifyDataSetChanged();
    adapteraconfirmer.notifyDataSetChanged();
}

此代码正常运行,但我不想再使用android.R.layout.simple_list_item_1布局了。所以我用自定义布局构建了一个ArrayAdapter。 我的问题/问题如下:如何将onAgendaDataReady()创建的ArrayList传递给我的适配器?

我试着这样做:

    appointmentItemAdapter = new AppointmentItemAdapter(this);
    rdvFutursListView.setAdapter(appointmentItemAdapter);
    appointmentItemAdapter.add(adapterfuturs);

但是appointmentItemAdapter需要一个Appointment而不是一个ArrayList。

感谢您的帮助

---------------最终解决方案-----------------

AppointmentItemAdapter appointmentFutursItemAdapter = new AppointmentItemAdapter(AgendaActivity.this);
rdvFutursListView.setAdapter(appointmentFutursItemAdapter);
appointmentFutursItemAdapter.addAll(appointmentsfuturs);
appointmentFutursItemAdapter.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:0)

我认为你在这里混合不同的东西。

您的自定义适配器必须将ArrayList(或任何Collection)作为输入参数并将当前项返回到列表(基于查询的位置)

因此仅将单个约会作为参数传递是不够的。