Reputation: 579
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.
Upvotes: 0
Views: 1831
Reputation: 10810
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();
}
});
Upvotes: 2
Reputation: 7635
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.
Upvotes: 0
Reputation: 9005
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
Upvotes: 0