0

I am a new android developer . I have create a quiz application where i need to display the questions in list view serially . my all question and options are images. how can i set the images in list view. i want to set only the image in list view not any text . Please help me.

dulal_026
  • 589
  • 1
  • 6
  • 20

4 Answers4

2

Create a listview in your main xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/masterLayout"
 android:orientation="vertical" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
    <ListView
     android:id="@+id/list"
     android:cacheColorHint="#00000000"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>
</LinearLayout>

Then create another xml file called child_layout:

<?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">
     <ImageView android:id="@+id/image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
</RelativeLayout>

Then in your activity class initialize your listview:

ListView listView1 = (ListView)findViewById(R.id.list);

Create a class that extends baseadapter and modify all the necessary methods the way you need to (create the constructor that takes a list of drawables as an argument and create a global variable that is set to the provided list). Then do the following in your activity class:

ArrayList<Drawable> images = new ArrayList<Drawable>();
// add to the list here
CustomListAdapter adapter = new CustomListAdapter(images);
listView1.setAdapter(adapter);

Do this in your getView() function in your customlistadapter class:

public View getView(int position, View convertView, ViewGroup parent)
{
    Drawable image = images.get(position);
    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_layout, null);
    }
    ImageView imageView = (ImageView)convertView.findViewById(R.id.image);
    imageView.setBackgroundDrawable(image);

    return convertView;
}

ListView item click listener:

listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
    public void onItemClick(AdapterView<?> listView, View itemView, int position, long itemId)
    {
        String message = "example text: " + position;
        Toast.makeText(MyActivity.this, message, Toast.LENGTH_SHORT).show();
    }
});
A. Abiri
  • 10,750
  • 4
  • 30
  • 31
  • Hi. where do I get the 'context' variable? – Foyzul Karim Jul 23 '11 at 09:51
  • Like the list of images, you also have to pass your activity's context as an argument to the constructor of the list adapter. Then in the list adapter, create a global Context variable and set it to the one that was provided to you in the constructor. – A. Abiri Jul 23 '11 at 19:08
  • I did it. Asked just for confirmation. :) – Foyzul Karim Jul 24 '11 at 04:09
  • Hi, Now I am able to set image in list view And I want to set on click listener to the list view.I want to set a toast to display the position of the image tapped.How can i do that? – dulal_026 Jul 24 '11 at 05:22
  • Create the listView1.onItemClicked() event in your activity. One of the variables given to you when the method is called is an int that is the position of the view. Just call Toast.makeText(MyActivity.this, message + position, Toast.LENGTH_SHORT).show(). – A. Abiri Jul 24 '11 at 09:00
0

You need to create a Custom ListAdapter. An Example is give in API samples. Just add an ImageView as the layout of the custom adapter and you should get going. Search more for the examples.

PravinCG
  • 7,688
  • 3
  • 30
  • 55
0

take one main layout xml file in which you have to give the . something like this

     <List
       android:width="wrap_content"
       android:height="wrap_content"
       android:id="@+id/list"
  />

and take another layout xml file with

<ImageView
     android:width="wrap_content"
     android:height="wrap_content"/>

In the adapter inflate this xml file that contains ImageView. so that you can get image in the list

Abhi
  • 8,935
  • 7
  • 37
  • 60
0

look at this tutorial... and replace textview with image view and set images to it.

also have a look at this question...

Hope this helps.

Thanks.

Community
  • 1
  • 1
N-JOY
  • 10,344
  • 7
  • 51
  • 69