Hi, this is my first tutorial here. I hope you’ll enjoy it. Ok, let’s start.
Let’s say that we want to create a Listview more different. We do not want to have only a small text, we want a button , a checkbox etc. in every Listview item. Here is a small tutorial where you can learn how to create an Adapter, the ” thing ” that will help us to personalize the ListView.
Ok, here is a small project where we’ll have:
-two classes : Listview.class (the class where we have the Listview ) and the ListviewAdapter.class ( the class which cantains the adapter for the Listview, with its help will populate the Listview as we want )
-two XML files : one where is the Listview and one where is the “design ” for each item. I’ve named them listview.xml and listview_row.xml .
I’ve decided to create an Adapter for a Listview whose each item will contain two Textviews , one for Firstname other for Lastname.Here is the content that we’re interested in. I will explain it by adding comments in the Java code.
Listview.class :
public class Listview extends Activity { //Here are variables which contains the unique "keys", one for every text in a single Listview row. static final String KEY_FIRSTNAME = "firstname",KEY_LASTNAME="lastname"; //Here are two arrays which contains some "interesting" words. Sorry, I had no inspiration. String[] firstname = {"Gates","Cheese","Apple","Computer","Noidea","Something","Bestintheworld"}; String[] lastname = {"Tom","Dan","Andrew","Tyler","Austin","Jane","Bill"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview); //We declare the Listview. I had so much inspiration that I called it - listview .Yes, amazing. ListView listview = (ListView) findViewById(R.id.lvGenuine); //This arraylist stores all the the information that will be in the ListView ArrayList<HashMap<String, String>> myArrayList = new ArrayList<HashMap<String, String>>(); //I need this " lenth" for the "for " part. int length = firstname.length; //I guess you know what this is. for(int i=0;i<length;i++){ // This hashmap stores the information that exists only in the item. HashMap<String, String> blablabla = new HashMap<String, String>(); blablabla.put(KEY_FIRSTNAME, firstname[i]); blablabla.put(KEY_LASTNAME, lastname[i]); //we save the hashmap in the ArrayList . Big thing retains the small one. myArrayList.add(blablabla); } //We declare the adapter using the other class ListviewAdapter adapter = new ListviewAdapter(this,myArrayList); //We set the adapter, here is the "place" where we populate the List. listview.setAdapter(adapter); } }
ListviewAdapter.class:
public class ListviewAdapter extends BaseAdapter {//To create an adapter we have to extend BaseAdapter instead of Activity, or whatever. private Activity activity; private ArrayList<HashMap<String, String>> data; private static LayoutInflater inflater=null; public ListviewAdapter(Activity a, ArrayList<HashMap<String, String>> d) { activity = a; data=d; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { //get the number of elements in the listview return data.size(); } public Object getItem(int position) { //this method returns on Object by position return position; } public long getItemId(int position) { //get item id by position return position; } public View getView(int position, View convertView, ViewGroup parent) { //getView method is the method which populates the listview with our personalized rows View vi=convertView; if(convertView==null) vi = inflater.inflate(R.layout.listview_row, null); //every item in listview uses xml "listview_row"'s design TextView firstname = (TextView)vi.findViewById(R.id.tvFirstname); //Here are two textviews in the listview_row xml file TextView lastname = (TextView)vi.findViewById(R.id.tvLastname); // You can enter anything you want, buttons, radiobuttons, images, etc. HashMap<String, String> hash = new HashMap<String, String>(); //We need a HashMap to store our data for any item hash = data.get(position); firstname.setText(hash.get(Listview.KEY_FIRSTNAME)); //We personalize our row's items. lastname.setText(hash.get(Listview.KEY_LASTNAME)); return vi; } }
listview.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lvGenuine" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
and finally listview_row.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvFirstname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Firstname" android:layout_marginLeft="20dip" android:textSize="25dip" /> <TextView android:id="@+id/tvLastname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Lastname" android:layout_marginLeft="40dip" android:textSize="15dip" /> </LinearLayout>
Here is a screenshot where you can see how it looks like :
You can download the entire project from here .
I hope you enjoyed the tutorial. I wait any kind of reviews and critics which I promise I will read and will think about them. If you’d like to see any tutorial for Android Development on www.androidgeniune.com you can leave a comment with your wish and I’ll try to help you.
This is all for now. Thank you for reading. See you !


