0

我想要两个在列表视图的每一行中显示两个图像和两个文本视图。我已经尝试过了,但没有显示图像。

这是自定义列表视图源代码,custom_listview.xml

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


<RelativeLayout
    android:id="@+id/custom_ListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:padding="20dp" >

    <ImageView
        android:id="@+id/imgBook1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:src="@drawable/book1" />

    <TextView
        android:id="@+id/txtTitleBook1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgBook1"
        android:text="Book1 Title Here" />

    <ImageView
        android:id="@+id/imgBook2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_toRightOf="@+id/imgBook1"
        android:src="@drawable/book2" />

    <TextView
        android:id="@+id/txtTitleBook2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtTitleBook1"
        android:layout_alignBottom="@+id/txtTitleBook2"
        android:layout_toRightOf="@+id/txtTitleBook1"
        android:text="Book2 Title Here" />

</RelativeLayout>

</RelativeLayout>

这是main.xml

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

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

这是我的Java代码Main.java

    package my.islamic.books.lib.app;

    import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Home extends ListActivity {

    String[] booksTitles = { "Book1", "Book2", "Book3", "Book4" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        setListAdapter(new MyAdapter(this, android.R.id.text1, booksTitles));

    }

    private class MyAdapter extends ArrayAdapter<String> {

        int[] booksCoverPhotos = { R.id.imgBook1, R.id.imgBook2, R.id.imgBook1,
                R.id.imgBook2};

        public MyAdapter(Context context, int resource, String[] objects) {
            // super(context, resource, textViewResourceId, objects);
            super(context, resource, objects);

        }

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

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = inflater.inflate(R.layout.listview_body, parent, false);

            ImageView imgView_book1 = (ImageView) row
                    .findViewById(R.id.imgBook1);
            ImageView imgView_book2 = (ImageView) row
                    .findViewById(R.id.imgBook2);
            TextView txtView_bookTitle1 = (TextView) row
                    .findViewById(R.id.txtTitleBook1);
            TextView txtView_bookTitle2 = (TextView) row
                    .findViewById(R.id.txtTitleBook2);

            txtView_bookTitle1.setText(booksTitles[position]);
            imgView_book1.setImageResource(booksCoverPhotos[position]);
            imgView_book2.setImageResource(booksCoverPhotos[position]);
            txtView_bookTitle2.setText(booksTitles[position]);

            return row;
        }

    }// end class

}

而且,当我在上面的 Java 代码中对任何 imgView_book1 或 imgView_book2 进行评论时,它会在 listView 的每一行中显示一个图像,但为什么不是两个图像呢?

4

2 回答 2

2

ImageView 方法setImageResource(int resId)获取可绘制对象的 id 作为参数。例如,R.drawable.your_image。阅读文档。

于 2012-10-30T08:21:33.557 回答
0

您可以使用 BaseAdapter 来实现您的要求,请参见此处此处

于 2012-10-30T08:21:50.637 回答