In this Example we show how to Add Multiple View in the Single Row of the ListView Control. We take two Controls ImageView and TextView as Shown follows.
- First We Create the layout file for the following that is as fllows : res/layout/rawlayout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_width="wrap_content" android:src="@drawable/icon" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:layout_marginLeft="10px" android:layout_marginTop="10px" android:textSize="20px" ></TextView> </LinearLayout>
- Then We Create The Custom ArrayAdapter Class Which will create by extending ArrayAdapter Class that has a getView() method that use to set how we want customize our row layout of for each raw of ListView
package com.container; import android.content.Context; 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 MyArrayAdapter extends ArrayAdapter<String> { Context context; String[] values; public MyArrayAdapter(Context context, String[] values) { super(context, R.layout.rowlayout, values); this.context = context; this.values = values; } public View getView(int positon, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.rowlayout, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.textView1); ImageView image = (ImageView) rowView.findViewById(R.id.imageView1); textView.setText(values[positon]); if (positon % 2 == 0) { image.setImageResource(R.drawable.yes); } else { image.setImageResource(R.drawable.no); } return rowView; } }
- Then We create the Our Activity Class which extends the ListActivity class in which we have set the Adapter of using the method setListAdapter() which has an argument of our own MyArrayAdapter class instance.
package com.container; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.Toast; public class MultipleViewListActivity extends ListActivity{ String [] values= {"Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2"};; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new MyArrayAdapter(getApplicationContext(), values)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), values[position], 5).show(); //super.onListItemClick(l, v, position, id); } }