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.

4 Answers 4

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();
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

Hi. where do I get the 'context' variable?
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.
I did it. Asked just for confirmation. :)
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?
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().
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.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.