android:如何在列表视图中添加对象的ArrayList(但不是所有属性)?

时间:2016-10-04 16:57:09

标签: java android listview

我创建了一个实体类 EventDto

public class EventDTO {
private String eventSourceName;
private String eventTime;
private String eventReason;
private String eventName;


public String getEventSourceName() {
    return eventSourceName;
}

public void setEventSourceName(String eventSourceName) {
    this.eventSourceName = eventSourceName;
}

public String getEventTime() {
    return eventTime;
}

public void setEventTime(String eventTime) {
    this.eventTime = eventTime;
}

public String getEventReason() {
    return eventReason;
}

public void setEventReason(String eventReason) {
    this.eventReason = eventReason;
}

public String getEventName() {
    return eventName;
}

public void setEventName(String eventName) {
    this.eventName = eventName;
}

@Override
public String toString() {
    return "EventDTO{" +
            ", eventSourceName='" + eventSourceName + '\'' +
            ", eventTime='" + eventTime + '\'' +
            ", eventReason='" + eventReason + '\'' +
            ", eventName='" + eventName + '\'' +
            '}';
}

}

我创建了一个EventDTO类型的数组列表

public static ArrayList<EventDTO> eventDTOList = new ArrayList<EventDTO>();

我添加了很多引用的事件,比如轰动多个tymes。

  eventDTOList.add(eventDTO);

现在我有列表视图如下

 <LinearLayout
    android:id="@+id/notifications_popup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="120dp"
    android:orientation="vertical"
    android:background="@drawable/cell_shape_gradient"
    android:visibility="visible"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    >

    <RelativeLayout
        android:id="@+id/minimize_notifications"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="@drawable/notification_header_shape_color"
        android:clickable="true"
        >

        <ImageView
            android:id="@+id/cancel_notification"
            android:layout_width="20dp"
            android:layout_height="5dp"
            android:background="@drawable/cancel_notifications"
            android:clickable="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/event_notification"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
           />
    </RelativeLayout>



        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#fbfdfcfc"
            android:layout_marginLeft="1dp"
            android:layout_marginRight="1dp"
            >

            <ListView
                android:id="@+id/eventList"
                android:layout_width="match_parent"
                android:layout_height="match_parent
                android:scrollbarSize="3dp"
                android:smoothScrollbar="true"
                android:layout_marginRight="2dp"
                android:layout_marginLeft="2dp"
                android:scrollbarFadeDuration="3000"
                />
        </RelativeLayout>
</LinearLayout>

现在点击按钮我将在列表视图中添加数组列表

 final ListView listview = (ListView) findViewById(R.id.eventList);

                ArrayAdapter<EventDTO> adapter = new ArrayAdapter<EventDTO>(GladiusLiveViewActivity.this, R.layout.listview_text_style, eventDTOList);
                listview.setAdapter(adapter);

但结果包含类名和所有属性名称 我想只显示以下两个属性 2.eventSourceName 3.eventReason

怎么做。

2 个答案:

答案 0 :(得分:0)

您需要覆盖EventDTO toString()

像这样的东西

@Override
public String toString(){
return this.getEventSourceName() + " - " + this.getEventReason();
}

此致

答案 1 :(得分:0)

替换你的Pojo。并检查是否有帮助

public class EventDTO {
private String eventSourceName;
private String eventTime;
private String eventReason;
private String eventName;


public String getEventSourceName() {
    return eventSourceName;
}

public void setEventSourceName(String eventSourceName) {
    this.eventSourceName = eventSourceName;
}

public String getEventTime() {
    return eventTime;
}

public void setEventTime(String eventTime) {
    this.eventTime = eventTime;
}

public String getEventReason() {
    return eventReason;
}

public void setEventReason(String eventReason) {
    this.eventReason = eventReason;
}

public String getEventName() {
    return eventName;
}

public void setEventName(String eventName) {
    this.eventName = eventName;
}

@Override
public String toString() {
    return "EventDTO{" +
            "eventSourceName='" + eventSourceName + '\'' +
            ", eventReason='" + eventReason + '\'' +
            '}';
}
}