Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create ListView in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 308
    Comment on it

    To create ListView in your android app follow the steps mentioned below:-

     

    1) Define ListView in your xml file where you wish to show list view.

     

    activity_main.xml

    1. <LinearLayout
    2. xmlns:tools="http://schemas.android.com/tools"
    3. xmlns:android="http://schemas.android.com/apk/res/android"
    4. tools:context=".MainActivity"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:orientation="vertical">
    8. <!-- TODO: Update blank fragment layout -->
    9. <RelativeLayout
    10. android:layout_marginTop="20dp"
    11. android:layout_marginLeft="10dp"
    12. android:layout_marginRight="10dp"
    13. android:layout_width="match_parent"
    14. android:layout_height="match_parent">
    15. <ListView
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content"
    18. android:id="@+id/list_of_items"/>
    19. </RelativeLayout>
    20.  
    21. </LinearLayout>
    22.  

     

    2) Now create a layout for the content that is to be shown in the list view , in simple words how our list view would appear is being set with this layout.

    mlist_view.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:layout_marginLeft="20dp"
    4. android:layout_marginTop="10dp"
    5. android:background="#f66"
    6. android:layout_width="match_parent" android:layout_height="match_parent"
    7. android:orientation="horizontal">
    8. <ImageView
    9. android:id="@+id/micon"
    10. android:layout_width="wrap_content"
    11. android:layout_height="wrap_content"
    12. />
    13. <LinearLayout
    14. android:layout_marginLeft="16dp"
    15. android:layout_width="wrap_content"
    16. android:layout_height="wrap_content"
    17. android:orientation="vertical">
    18. <TextView
    19. android:textSize="19dp"
    20. android:layout_width="wrap_content"
    21. android:layout_height="wrap_content"
    22. android:id="@+id/title"
    23. />
    24. <TextView
    25. android:textSize="19dp"
    26. android:layout_width="wrap_content"
    27. android:layout_height="wrap_content"
    28. android:id="@+id/description"
    29. />
    30. </LinearLayout>
    31. </LinearLayout>

    3) Now create an Adapter Class for the List view.

    MyAdapter.java

    1. public class MyAdapter extends BaseAdapter {
    2. LayoutInflater inflater=null;
    3. String[] mtitle;
    4. String[] mdiscription;
    5. int[] mimg;
    6. Context context;
    7. private TextView title,discription;
    8. private ImageView img;
    9. public MyAdapter(Context context1,String[] prgramTitle, String[] prgramNameList,int[] prgmImg)
    10. {
    11. mtitle=prgramTitle;
    12. mdiscription=prgramNameList;
    13. mimg=prgmImg;
    14. context=context1;
    15. inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    16. }
    17. @Override
    18. public int getCount() {
    19. return mtitle.length;
    20. }
    21.  
    22. @Override
    23. public Object getItem(int position) {
    24. return position;
    25. }
    26.  
    27. @Override
    28. public long getItemId(int position) {
    29. return position;
    30. }
    31.  
    32. @Override
    33. public View getView(int position, View convertView, ViewGroup parent) {
    34.  
    35. View myView=inflater.inflate(R.layout.mlist_view,null);
    36. title=(TextView)myView.findViewById(R.id.title);
    37. discription=(TextView)myView.findViewById(R.id.description);
    38. img=(ImageView)myView.findViewById(R.id.micon);
    39. title.setText(mtitle[position]);
    40. discription.setText(mdiscription[position]);
    41. img.setImageResource(mimg[position]);
    42. return myView;
    43. }
    44.  
    45. }
    46.  

    4) Now initialize the list view in your MainActivity file and set its adapter as MyAdapter.

    MainActivity.java

    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.  
    4. private ListView myListView;
    5. Context context;
    6. public static int [] prgmImages={R.drawable.ic_home,R.drawable.ic_pages,R.drawable.ic_people,R.drawable.ic_whatshot,R.drawable.ic_photo,R.drawable.ic_home,R.drawable.ic_photo,R.drawable.ic_whatshot,R.drawable.ic_people};
    7. public static String [] prgmNameList={"Let you C","c+++","JAVA/OAK","Jsp/Server App","Microsoft's.Net","Android The Best","PHP/Server","Jquery","JavaScript"};
    8. public static String [] prgmNameMyList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
    9.  
    10.  
    11.  
    12.  
    13. protected void onCreate(Bundle savedInstanceState) {
    14. super.onCreate(savedInstanceState);
    15. setContentView(R.layout.activity_main);
    16.  
    17.  
    18. myListView=(ListView)findViewById(R.id.list_of_items);
    19. myListView.setAdapter(new MyAdapter(context,prgmNameList,prgmNameMyList,prgmImages));
    20.  
    21. }}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: