Question

I want two display two Images and and two Text Views in each row of the List View. I have tried this but images are not displayed.

This is code of Customized list view source, 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>

and here is 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>

And here is my Java code, 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

}

And also when I'm making comment any imgView_book1 or imgView_book2 in the above Java code, then it's displaying one image in each row of listView, but why not both images?

Was it helpful?

Solution

The ImageView method setImageResource(int resId) gets an id of your drawable as a parameter. For example, R.drawable.your_image. Read this document.

OTHER TIPS

You can Use BaseAdapter to Achive Your requirements see Here And Here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top