在自定义列表视图中显示多个元素

时间:2016-02-21 14:29:25

标签: java android listview

目前我有一个自定义列表视图,我想在每行中显示图片名称和主题。每行中的编辑文本仅显示名称而不是主题

public class BookAdapter extends ArrayAdapter<String>
{
    BookAdapter(Context context, String[] books, String[] subjects)
    {
        super(context, R.layout.bookrow, books);


    }

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

        LayoutInflater booksinflater = LayoutInflater.from(getContext());
        View customView = booksinflater.inflate(R.layout.bookrow, parent, false);

        String singleBookItem = getItem(position);
        String singleBookSubject = getItem(position);
        TextView bookName = (TextView) customView.findViewById(R.id.BookName);
        ImageView bookImage = (ImageView) customView.findViewById(R.id.bookimage);
        TextView bookSubject = (TextView) customView.findViewById(R.id.BookSubject);


        bookName.setText(singleBookItem);
        bookImage.setImageResource(R.drawable.matter_of_fact);
        bookSubject.setText(singleBookSubject);

        return customView;
    }
}

我知道这与输入代码super(context, R.layout.bookrow, book);有关,但如何将其更改为同时包含图书和主题?

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:weightSum="1"
              android:id="@+id/layout">

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/bookimage"
        android:src="@drawable/matter_of_fact"
        android:padding="0dp"
        android:layout_margin="0dp"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/BookName"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/BookSubject"
            android:padding="30dp"/>

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

@Override
// Return the book name for that position
public Object getItem(int position) {
    return books[position];
}

// Return the subject for that position
private String getSubject(int position) {
    return subjects[position];
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        LayoutInflater booksinflater = LayoutInflater.from(getContext());
        View customView = booksinflater.inflate(R.layout.bookrow, parent, false);
    }

    String singleBookItem = getItem(position);
    String singleBookSubject = getSubject(position);

    ...

    bookName.setText(singleBookItem);
    bookImage.setImageResource(R.drawable.matter_of_fact);
    bookSubject.setText(singleBookSubject);

    return customView;
}

您要为两者设置相同的文字:主题和书名。 创建一些返回主题的附加方法...

类似的东西:

stdin