通过自定义适配器设置多个TextView的文本

时间:2017-02-17 13:05:34

标签: android android-adapter

我在使用自定义适配器在自定义列表布局中设置多个textview的文本时遇到问题。这是我的列表项布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >


<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dp"
    android:contentDescription="TODO"
    android:src="@drawable/blue_circle" />


<TextView
    android:id="@+id/short_content"
    android:layout_width="wrap_content"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:text="content"
    android:singleLine="true"
    android:textSize="12sp" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/short_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Title"
    android:textSize="16sp" />

<TextView
    android:id="@+id/date"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/short_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="right"
    android:text="date"
    android:textSize="13sp" />

这是我的适配器:

public class MessageAdapter extends ArrayAdapter<TextContent> {


public MessageAdapter(Context context, int textViewResourceId){
    super(context, textViewResourceId);
}

public MessageAdapter(Context context, int textViewResourceId, List<TextContent> items){
    super(context, textViewResourceId, items);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (view == null) {
        LayoutInflater inflater;
        inflater = LayoutInflater.from(getContext());
        view = inflater.inflate(R.layout.messages_layout, null);
    }

    TextContent p = getItem(position);

    if(p != null) {
        TextView textViewShortContent = (TextView) view.findViewById(R.id.short_content);
        TextView textViewTitle = (TextView) view.findViewById(R.id.title);
        TextView textViewDate = (TextView) view.findViewById(R.id.date);

        if (textViewShortContent != null) {
            textViewShortContent.setText("Short Content");
        }

        if (textViewTitle != null) {
            textViewTitle.setText("TITLE");
        }

        if (textViewDate != null) {
            textViewDate.setText("DATE");
        }
    }

    return view;
}

}

这是我的textContent类:

public class TextContent implements Serializable{

private String GUID, created, modified, deleted, name, message, title, shortContent, fullContent, author, language, person;

public TextContent(){

}

public TextContent(String GUID, String created, String modified, String deleted, String name, String message, String title,
                   String shortContent, String fullContent, String author, String language, String person) {
    this.GUID = GUID;
    this.created = created;
    this.modified = modified;
    this.deleted = deleted;
    this.name = name;
    this.message = message;
    this.title = title;
    this.shortContent = shortContent;
    this.fullContent = fullContent;
    this.author = author;
    this.language = language;
    this.person = person;
}

public String getGUID() {
    return GUID;
}

public String getCreated() {
    return created;
}

public String getModified() {
    return modified;
}

public String getDeleted() {
    return deleted;
}

public String getName() {
    return name;
}

public String getMessage() {
    return message;
}

public String getTitle() {
    return title;
}

public String getShortContent() {
    return shortContent;
}

public String getFullContent() {
    return fullContent;
}

public String getAuthor() {
    return author;
}

public String getLanguage() {
    return language;
}

public String getPerson() {
    return person;
}

public void setGUID(String GUID) {
    this.GUID = GUID;
}

public void setCreated(String created) {
    this.created = created;
}

public void setModified(String modified) {
    this.modified = modified;
}

public void setDeleted(String deleted) {
    this.deleted = deleted;
}

public void setName(String name) {
    this.name = name;
}

public void setMessage(String message) {
    this.message = message;
}

public void setTitle(String title) {
    this.title = title;
}

public void setShortContent(String shortContent) {
    this.shortContent = shortContent;
}

public void setFullContent(String fullContent) {
    this.fullContent = fullContent;
}

public void setAuthor(String author) {
    this.author = author;
}

public void setLanguage(String language) {
    this.language = language;
}

public void setPerson(String person) {
    this.person = person;
}

}

这是我实例化我的适配器

的地方
listView = (ListView) getView().findViewById(R.id.list_view_messages);
    MessageAdapter adapter = new MessageAdapter(context, R.layout.messages_layout, textContentList);
    listView.setAdapter(adapter);

显示的唯一内容是getShortContent,但由于某种原因,不是getTitle或getCreated。我100%确定已经为textContent的所有字段设置了文本。

1 个答案:

答案 0 :(得分:0)

我明白了。我的布局中的textViews重叠,因此其他textview的内容隐藏在后面。