I have created a simple list consisting an ImageView and a TextView without using custom adapter class. I poked around the internet and found that most of the custom lists are created using a seperate adapter class... Here is my layout for the list row...
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_marginRight="15dp"/>
<TextView android:id="@+id/list_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="15sp"/>
</LinearLayout>
MainActivity.java
//Taking reference of a ListView
ListView listView = (ListView) findViewById(R.id.list_view);
//Setting up ArrayList to feed ArrayAdapter
ArrayList<String> fruits= new ArrayList<String>();
fruits.add("Apple");
fruits.add("Mango");
fruits.add("Strawberries");
fruits.add("Grapes");
//Setting up ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, R.layout.list_row, R.id.list_item_id, fruits);
listView.setAdapter(adapter);
The above code is working fine... I just want to know when to create the seperate Adapter class and what is the use of it? In other words, what is the benefit of creating a seperate Adapter class? Any help will be appreciated... Thanks in advance...
Are you even setting the TextView
and the ImageView
for each row in your ListView
? Because it seems like you're not. You're giving every row in your list the same image and the text is being set by the Adapter.
If you would want to send multiple Lists
to your ArrayAdapter
, you would have to write your own Adapter. Like when you fetch images and text for your ListView
. You would have to write your own ArrayAdapter
that will set the text and the images, by inflating them into every rowView
with the corresponding text and image.
Also this goes for more complex UI for each row, like this one:
(source: addictivetips.com)
Each row has multiple TextViews
and Buttons
and an ImageView
inflated from a row.xml