RSS

Android – Multi Column ListView

06 Feb

Problem: How do i display multi-columnr listview in Android?
Description:
I know many novice android programmer are facing problem to implement multi-column listview or in confusion to implement this kind of view. so let me write here to implement multi columnr listview by using ListView itself.

Yes, its little bit tricky but if you have gone through my previous articles for creating Custom ListView then you will sure realize that its just a difference of creating listView_row xml layout file. Just go through the above link.

Output:
Android - Multi column ListViewAndroid – Multi column ListView

Solution:
I haven’t done any magic but created a listview_row.xml as:

Android - Multi column Listview rowAndroid – Multi column Listview row

And now you can define the custom adapter for this listview same as our practice of defining custom adapter for ListView.

listview_row.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:id=”@+id/relativeLayout1″ android:layout_width=”fill_parent”
  4. android:layout_height=”fill_parent” >
  5. <TextView
  6. android:id=”@+id/FirstText” android:layout_width=”0dp”
  7. android:layout_height=”wrap_content” android:layout_weight=”1″
  8. android:text=”First”/>
  9. <TextView
  10. android:id=”@+id/SecondText” android:layout_width=”0dp”
  11. android:layout_height=”wrap_content” android:layout_weight=”2″
  12. android:text=”Second”/>
  13. <TextView
  14. android:id=”@+id/ThirdText” android:layout_width=”0dp”
  15. android:layout_height=”wrap_content” android:layout_weight=”1″
  16. android:text=”Third”/>
  17. <TextView
  18. android:id=”@+id/FourthText” android:layout_width=”0dp”
  19. android:layout_height=”wrap_content” android:layout_weight=”1″
  20. android:text=”Fourth”/>
  21. </LinearLayout>

main.xml

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:orientation=”vertical” >
  6. <ListView
  7. android:id=”@+id/listview”
  8. android:layout_height=”match_parent”
  9. android:layout_width=”match_parent”/>
  10. </LinearLayout>

listviewAdapter.java

  1. package com.paresh.demoexample;
  2. import static com.paresh.demoexample.Constant.FIRST_COLUMN;
  3. import static com.paresh.demoexample.Constant.SECOND_COLUMN;
  4. import static com.paresh.demoexample.Constant.THIRD_COLUMN;
  5. import static com.paresh.demoexample.Constant.FOURTH_COLUMN;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import android.app.Activity;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseAdapter;
  13. import android.widget.TextView;
  14. public class listviewAdapter extends BaseAdapter
  15. {
  16. public ArrayList<HashMap<String,String>> list;
  17. Activity activity;
  18. public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
  19. super();
  20. this.activity = activity;
  21. this.list = list;
  22. }
  23. @Override
  24. public int getCount() {
  25. // TODO Auto-generated method stub
  26. return list.size();
  27. }
  28. @Override
  29. public Object getItem(int position) {
  30. // TODO Auto-generated method stub
  31. return list.get(position);
  32. }
  33. @Override
  34. public long getItemId(int position) {
  35. // TODO Auto-generated method stub
  36. return 0;
  37. }
  38. private class ViewHolder {
  39. TextView txtFirst;
  40. TextView txtSecond;
  41. TextView txtThird;
  42. TextView txtFourth;
  43. }
  44. @Override
  45. public View getView(int position, View convertView, ViewGroup parent) {
  46. // TODO Auto-generated method stub
  47. // TODO Auto-generated method stub
  48. ViewHolder holder;
  49. LayoutInflater inflater = activity.getLayoutInflater();
  50. if (convertView == null)
  51. {
  52. convertView = inflater.inflate(R.layout.listview_row, null);
  53. holder = new ViewHolder();
  54. holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
  55. holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
  56. holder.txtThird = (TextView) convertView.findViewById(R.id.ThirdText);
  57. holder.txtFourth = (TextView) convertView.findViewById(R.id.FourthText);
  58. convertView.setTag(holder);
  59. }
  60. else
  61. {
  62. holder = (ViewHolder) convertView.getTag();
  63. }
  64. HashMap<String, String> map = list.get(position);
  65. holder.txtFirst.setText(map.get(FIRST_COLUMN));
  66. holder.txtSecond.setText(map.get(SECOND_COLUMN));
  67. holder.txtThird.setText(map.get(THIRD_COLUMN));
  68. holder.txtFourth.setText(map.get(FOURTH_COLUMN));
  69. return convertView;
  70. }
  71. }

MultiColumnActivity.java

  1. package com.paresh.demoexample;
  2. import static com.paresh.demoexample.Constant.FIRST_COLUMN;
  3. import static com.paresh.demoexample.Constant.SECOND_COLUMN;
  4. import static com.paresh.demoexample.Constant.THIRD_COLUMN;
  5. import static com.paresh.demoexample.Constant.FOURTH_COLUMN;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.ListView;
  11. public class MultiColumnActivity extends Activity
  12. {
  13. private ArrayList<HashMap<String,String>> list;
  14. public void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. ListView lview = (ListView) findViewById(R.id.listview);
  19. populateList();
  20. listviewAdapter adapter = new listviewAdapter(this, list);
  21. lview.setAdapter(adapter);
  22. }
  23. private void populateList() {
  24. list = new ArrayList<HashMap<String,String>>();
  25. HashMap<String,String> temp = new HashMap<String,String>();
  26. temp.put(FIRST_COLUMN,”Colored Notebooks”);
  27. temp.put(SECOND_COLUMN, ”By NavNeet”);
  28. temp.put(THIRD_COLUMN, ”Rs. 200″);
  29. temp.put(FOURTH_COLUMN, ”Per Unit”);
  30. list.add(temp);
  31. HashMap<String,String> temp1 = new HashMap<String,String>();
  32. temp1.put(FIRST_COLUMN,”Diaries”);
  33. temp1.put(SECOND_COLUMN, ”By Amee Products”);
  34. temp1.put(THIRD_COLUMN, ”Rs. 400″);
  35. temp1.put(FOURTH_COLUMN, ”Per Unit”);
  36. list.add(temp1);
  37. HashMap<String,String> temp2 = new HashMap<String,String>();
  38. temp2.put(FIRST_COLUMN,”Note Books and Stationery”);
  39. temp2.put(SECOND_COLUMN, ”By National Products”);
  40. temp2.put(THIRD_COLUMN, ”Rs. 600″);
  41. temp2.put(FOURTH_COLUMN, ”Per Unit”);
  42. list.add(temp2);
  43. HashMap<String,String> temp3 = new HashMap<String,String>();
  44. temp3.put(FIRST_COLUMN,”Corporate Diaries”);
  45. temp3.put(SECOND_COLUMN, ”By Devarsh Prakashan”);
  46. temp3.put(THIRD_COLUMN, ”Rs. 800″);
  47. temp3.put(FOURTH_COLUMN, ”Per Unit”);
  48. list.add(temp3);
  49. HashMap<String,String> temp4 = new HashMap<String,String>();
  50. temp4.put(FIRST_COLUMN,”Writing Pad”);
  51. temp4.put(SECOND_COLUMN, ”By TechnoTalaktive Pvt. Ltd.”);
  52. temp4.put(THIRD_COLUMN, ”Rs. 100″);
  53. temp4.put(FOURTH_COLUMN, ”Per Unit”);
  54. list.add(temp4);
  55. }
  56. }

Constant.java

  1. package com.paresh.demoexample;
  2. public class Constant {
  3. public static final String FIRST_COLUMN = ”First”;
  4. public static final String SECOND_COLUMN = ”Second”;
  5. public static final String THIRD_COLUMN = ”Third”;
  6. public static final String FOURTH_COLUMN = ”Fourth”;
  7. }

Download full Example from Here: Android – Multi Column ListView

Please feel free to give comment/feedback if you are knowing better solutions than this. :)

 

Referenced by: http://www.technotalkative.com/android-multi-column-listview/

 

About sochinda

I really like new technology!!
2 Comments

Posted by on February 6, 2012 in Android

 

2 Responses to Android – Multi Column ListView

  1. Gayathri

    February 18, 2012 at 5:59 PM

    I need add data to listview from edittexts using add button.The Listview should be multi columns. Please help me.

     
  2. honey

    May 23, 2012 at 12:22 PM

    i guess u copy it from http://www.technotalkative.com/android-multi-column-listview/ but forget to change package name …………….
    nice!!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.