Java 使用数组适配器将多个值从对象分配给listview中的单个项

Java 使用数组适配器将多个值从对象分配给listview中的单个项,java,android,listview,android-listview,jodatime,Java,Android,Listview,Android Listview,Jodatime,我有一个约会对象,其中包含几个字段;主题、开始时间、结束时间等 我正在尝试获取这些对象的列表以显示在listview中。我有一个列表项布局xml文件,其中包含4个TextView对象;开始时间、结束时间、主题和人名。布局如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

我有一个
约会
对象,其中包含几个字段;主题、开始时间、结束时间等

我正在尝试获取这些对象的列表以显示在listview中。我有一个列表项布局xml文件,其中包含4个
TextView
对象;开始时间、结束时间、主题和人名。布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/lblTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignBaseline="@+id/lblSubject"
        android:layout_alignBottom="@+id/lblSubject"
        android:paddingTop="16dp"
        android:paddingLeft="16dp"
        android:text="Time"
        android:textSize="16dp"
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/lblSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Subject"
        android:textSize="20dp"
        android:paddingTop="16dp"
        android:paddingRight="16dp"
        android:textColor="#FFFFFF" /> 

    <TextView
        android:id="@+id/lblLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/lblCustomer"
        android:layout_alignBottom="@+id/lblCustomer"
        android:layout_alignParentLeft="true"
        android:text="Location"
        android:paddingBottom="15dp"
        android:paddingLeft="16dp"
        android:textColor="#FFFFFF"
        android:textSize="16dp" />  

    <TextView
        android:id="@+id/lblCustomer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/lblSubject"
        android:text="Customer"
        android:paddingBottom="15dp"
        android:paddingRight="16dp"
        android:textSize="16dp"
        android:textColor="#FFFFFF" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/lblLocation"
        android:background="#333333" />

</RelativeLayout>
到目前为止,我的代码是:

SimpleCursorAdapter cAdapter;
DiaryAdapter dAdapter;
DateTime currentDate;
ListView diary;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Show the Up button in the action bar.
    setupActionBar();
    setContentView(R.layout.activity_diary);
    diary = (ListView) findViewById(R.id.lstAppts);

    currentDate = new DateTime();
    populateDiaryNew(currentDate);
}

private void populateDiaryNew(DateTime dt) {
    currentDate = dt;
    ArrayList<Appointment> appointments = new ArrayList<Appointment>();
    Cursor cursor = Db.Functions.getAppointmentList(dt);
    if (cursor == null)
        return;
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Appointment appt = Db.Functions.getAppointment(cursor.getString(cursor.getColumnIndex("_id")));
        appointments.add(appt);
        cursor.moveToNext();
    }
    cursor.close();

    ListIterator<Appointment> iter = appointments.listIterator();
    DateTime lastEndTime = new DateTime();
    int count = 0;
    while (iter.hasNext()){
        lastEndTime = iter.next().endDateTime;

        if (count > 0)
        {
            if (iter.next().startDateTime.isAfter(lastEndTime))
            {
                Appointment freeAppt = new Appointment();
                freeAppt.isFreeTime = true;
                freeAppt.subject = "Free slot";
                freeAppt.startDateTime = lastEndTime;
                freeAppt.endDateTime = iter.next().startDateTime;
                appointments.add(freeAppt);
            }
        }
        count++;
    }

    ArrayAdapter<Appointment> arrayAdapter = new ArrayAdapter<Appointment>(this, R.layout.appointment_info, R.id.lblSubject, appointments);
    diary.setAdapter(arrayAdapter);     
}
SimpleCursorAdapter地籍仪;
日记转接器;
日期时间当前日期;
列表视图日志;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//在操作栏中显示“向上”按钮。
setupActionBar();
setContentView(R.layout.activity_日记);
日记=(列表视图)findViewById(R.id.lstAppts);
currentDate=新的日期时间();
PopulatedArynew(当前日期);
}
private void PopulatedArynew(日期时间dt){
currentDate=dt;
ArrayList约会=新建ArrayList();
Cursor=Db.Functions.getAppointmentList(dt);
if(游标==null)
返回;
cursor.moveToFirst();
而(!cursor.isAfterLast()){
约会appt=Db.Functions.getAppointment(cursor.getString(cursor.getColumnIndex(“\u id”));
任命.增补(附件);
cursor.moveToNext();
}
cursor.close();
ListIterator iter=约会。ListIterator();
DateTime lastEndTime=新日期时间();
整数计数=0;
while(iter.hasNext()){
lastEndTime=iter.next().endDateTime;
如果(计数>0)
{
if(iter.next().startDateTime.isAfter(lastEndTime))
{
约会freeAppt=新约会();
freeapp.isFreeTime=true;
freeapp.subject=“免费插槽”;
freeApp.startDateTime=lastEndTime;
freeApp.endDateTime=iter.next().startDateTime;
任命。添加(freeAppt);
}
}
计数++;
}
ArrayAdapter ArrayAdapter=新的ArrayAdapter(this,R.layout.appointment\u info,R.id.lblSubject,appoints);
日记。设置适配器(arrayAdapter);
}

问题是,
ArrayAdapter
似乎只将一个字段调整到一个视图,但我需要将4个不同的字段(从一个
Appointment
对象)调整到4个不同的文本视图,所有这些都在一个listview项中。如何执行此操作?

您需要覆盖ArrayAdapter中的getView方法,以手动将每行充气到您想要的方式。默认情况下,ArrayAdapter只调用对象上的toString()来显示

与此类似:

代码示例:

static class ViewHolder {
    TextView v1;
    TextView v2;
    TextView v3;
    TextView v4;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Appointment temp = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) { //inflate convertView and populate viewHolder
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.yourLayout, parent, false);
        viewHolder.v1= (TextView) convertView.findViewById(R.id.lblTime);
        viewHolder.v2= (TextView) convertView.findViewById(R.id.lblSubject);
        viewHolder.v3= (TextView)convertView.findViewById(R.id.lblLocation);
        viewHolder.v4= (TextView) convertView.findViewById(R.id.lblCustomer);
        convertView.setTag(viewHolder); //set the tag
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag(); //re-use the ViewHolder to     save calls to findViewById
    }
    viewHolder.v1.setText(temp.getText1());
    viewHolder.v2.setText(temp.getText2());
    viewHolder.v3.setText(temp.getText3());
    viewHolder.v4.setText(temp.getText4());
    return convertView;
}

您需要覆盖ArrayAdapter中的getView方法,以手动将每一行充气到您想要的方式。默认情况下,ArrayAdapter只调用对象上的toString()来显示

与此类似:

代码示例:

static class ViewHolder {
    TextView v1;
    TextView v2;
    TextView v3;
    TextView v4;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final Appointment temp = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) { //inflate convertView and populate viewHolder
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.yourLayout, parent, false);
        viewHolder.v1= (TextView) convertView.findViewById(R.id.lblTime);
        viewHolder.v2= (TextView) convertView.findViewById(R.id.lblSubject);
        viewHolder.v3= (TextView)convertView.findViewById(R.id.lblLocation);
        viewHolder.v4= (TextView) convertView.findViewById(R.id.lblCustomer);
        convertView.setTag(viewHolder); //set the tag
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag(); //re-use the ViewHolder to     save calls to findViewById
    }
    viewHolder.v1.setText(temp.getText1());
    viewHolder.v2.setText(temp.getText2());
    viewHolder.v3.setText(temp.getText3());
    viewHolder.v4.setText(temp.getText4());
    return convertView;
}

您可以向ArrayAdapter提供数组列表对象
约会
-

 arrayAdapter = new ArrayAdapter<ArrayList>(this, android.R.layout.simple_list_item_1, appointments);
listView.setAdapter(arrayAdapter);


 public class CustomAdapter extends ArrayAdapter<Appointment>{

ArrayList<Contact> items;

LayoutInflater mInflater ; 

Context context;

int layoutResourceId; 

public CustomAdapter(Context context, int layoutResourceId, ArrayList<Appointment> items) {
    super(context, layoutResourceId, items);

    this.layoutResourceId=layoutResourceId;

    this.items = items;

    this.context=context;
}.......
arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,约会);
setAdapter(arrayAdapter);
公共类CustomAdapter扩展了ArrayAdapter{
数组列表项;
拉平机;
语境;
国际布局资源;
公共CustomAdapter(上下文上下文、int-layoutResourceId、ArrayList项){
超级(上下文、布局资源ID、项目);
this.layoutResourceId=layoutResourceId;
这个项目=项目;
this.context=context;
}.......

然后覆盖getView()并展开列表项的视图,然后从指定位置的ArrayList对象项设置约会值。您可以向ArrayAdapter提供数组列表对象约会-

 arrayAdapter = new ArrayAdapter<ArrayList>(this, android.R.layout.simple_list_item_1, appointments);
listView.setAdapter(arrayAdapter);


 public class CustomAdapter extends ArrayAdapter<Appointment>{

ArrayList<Contact> items;

LayoutInflater mInflater ; 

Context context;

int layoutResourceId; 

public CustomAdapter(Context context, int layoutResourceId, ArrayList<Appointment> items) {
    super(context, layoutResourceId, items);

    this.layoutResourceId=layoutResourceId;

    this.items = items;

    this.context=context;
}.......
arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,约会);
setAdapter(arrayAdapter);
公共类CustomAdapter扩展了ArrayAdapter{
数组列表项;
拉平机;
语境;
国际布局资源;
公共CustomAdapter(上下文上下文、int-layoutResourceId、ArrayList项){
超级(上下文、布局资源ID、项目);
this.layoutResourceId=layoutResourceId;
这个项目=项目;
this.context=context;
}.......

然后覆盖getView()并膨胀列表项的视图,然后从指定位置从ArrayList对象项设置约会值

考虑使用SimpleCursor适配器而不是ArrayAdapter, 您可以在其中传递自定义视图并将数据绑定到该视图。
或者通过扩展ArrayAdapter来定制ArrayAdapter

考虑使用SimpleCorsor适配器而不是ArrayAdapter, 您可以在其中传递自定义视图并将数据绑定到该视图。 或者通过扩展ArrayAdapter来定制ArrayAdapter