Let's start with simple requirement.
A list view containing a image & 2 TextViews as below.
To achieve this one has to create an model of single list in a seperate layout & inflate it to listview.
Let's do it.
we get this with the above code.
Happy Coding :)
A list view containing a image & 2 TextViews as below.
To achieve this one has to create an model of single list in a seperate layout & inflate it to listview.
Let's do it.
Create a ListView in the View of our current layout. "activity_main.xml"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context=".MainActivity" > <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Create a customView "custom_list_view.xml"
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#EEE" android:minHeight="90dp" android:padding="4dp" tools:ignore="HardcodedText,ContentDescription" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:maxHeight="60dp" android:maxWidth="60dp" android:scaleType="fitXY" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:layout_marginTop="8dp" android:layout_toRightOf="@+id/imageView1" android:text="TextView1" android:textSize="21sp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignParentRight="true" android:layout_marginRight="16dp" android:text="TextView2" /> </RelativeLayout>
we get this with the above code.
Inflate this layout as adapter to our listView.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.list_view); CustomAdapter adapter = new CustomAdapter(this); mListView.setAdapter(adapter); }
Create a sub class CustomAdapter extending BaseAdapter, add unimplemented methods
public class CustomAdapter extends BaseAdapter { Activity sActivity; public CustomAdapter(final Activity mActivity) { this.sActivity = mActivity; } @Override public int getCount() { /* * Length of our listView */ count = MainActivity.textview1.length; //Refer Below return count; } @Override public Object getItem(int position) { /* * Current Item */ return position; } @Override public long getItemId(int position) { /* * Current Item's ID */ return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View mView = convertView; if (mView == null) { /* * LayoutInflater */ LayoutInflater sInflater = (LayoutInflater) sActivity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); /* * Inflate Custom List View */ mView = sInflater.inflate(R.layout.custom_list_view, null, false); } /* **************CUSTOM LISTVIEW OBJECTS**************** */ TextView sTV1 = (TextView) mView.findViewById(R.id.textView1); TextView sTV2 = (TextView) mView.findViewById(R.id.textView2); ImageView sIMG = (ImageView) mView.findViewById(R.id.imageView1); /* **************CUSTOM LISTVIEW OBJECTS**************** */ /* **************ADDING CONTENTS**************** */ sTV1.setText(MainActivity.textview1[position]); sTV2.setText(MainActivity.textview2[position]); sIMG.setImageResource(R.drawable.ic_launcher); /* **************ADDING CONTENTS**************** */ /* * Return View here */ return mView; }
Add OnItemClickListener to get clicked item
import android.widget.AdapterView.OnItemClickListener;
mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), textview1[position] + "\n" + textview2[position], Toast.LENGTH_SHORT) .show(); } });
Finally with Activity and CustomAdapter.
/** * Author : VenomVendor * Dated : 5 Dec, 2013 4:19:37 PM * Project : SimpleListView * Contact : info@VenomVendor.com * URL : https://www.google.co.in/search?q=VenomVendor **/ package vee.simplelistview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { ListView mListView; int count = 0; final static String[] textview1 = { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8" }; final static String[] textview2 = { "Age1", "Age2", "Age3", "Age4", "Age5", "Age6", "Age7", "Age8" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.list_view); final CustomAdapter adapter = new CustomAdapter(this); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), textview1[position] + "\n" + textview2[position], Toast.LENGTH_SHORT) .show(); } }); } /* * CustomAdapter */ public class CustomAdapter extends BaseAdapter { Activity sActivity; public CustomAdapter(final Activity mActivity) { this.sActivity = mActivity; } @Override public int getCount() { /* * Length of our listView */ count = MainActivity.textview1.length; return count; } @Override public Object getItem(int position) { /* * Current Item */ return position; } @Override public long getItemId(int position) { /* * Current Item's ID */ return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View mView = convertView; if (mView == null) { /* * LayoutInflater */ final LayoutInflater sInflater = (LayoutInflater) sActivity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); /* * Inflate Custom List View */ mView = sInflater.inflate(R.layout.custom_list_view, null, false); } /* **************CUSTOM LISTVIEW OBJECTS**************** */ /* * DO NOT MISS TO ADD "mView" */ final TextView sTV1 = (TextView) mView.findViewById(R.id.textView1); final TextView sTV2 = (TextView) mView.findViewById(R.id.textView2); final ImageView sIMG = (ImageView) mView.findViewById(R.id.imageView1); /* **************CUSTOM LISTVIEW OBJECTS**************** */ /* **************ADDING CONTENTS**************** */ sTV1.setText(MainActivity.textview1[position]); sTV2.setText(MainActivity.textview2[position]); sIMG.setImageResource(R.drawable.ic_launcher); /* **************ADDING CONTENTS**************** */ /* * Return View here */ return mView; } } }
SimpleListView on GitHub
Final Required View
Happy Coding :)