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
    • 286
    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

    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!-- TODO: Update blank fragment layout -->
        <RelativeLayout
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
       
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/list_of_items"/>
        </RelativeLayout>
    
    </LinearLayout>
    

     

    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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:background="#f66"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/micon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <LinearLayout
            android:layout_marginLeft="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <TextView
            android:textSize="19dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            />
            <TextView
                android:textSize="19dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/description"
                />
        </LinearLayout>
    </LinearLayout>

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

    MyAdapter.java

    public class MyAdapter extends BaseAdapter {
        LayoutInflater inflater=null;
        String[] mtitle;
        String[] mdiscription;
        int[] mimg;
        Context context;
        private TextView title,discription;
        private ImageView img;
        public MyAdapter(Context context1,String[] prgramTitle, String[] prgramNameList,int[] prgmImg)
        {
            mtitle=prgramTitle;
            mdiscription=prgramNameList;
            mimg=prgmImg;
            context=context1;
            inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return mtitle.length;
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View myView=inflater.inflate(R.layout.mlist_view,null);
            title=(TextView)myView.findViewById(R.id.title);
            discription=(TextView)myView.findViewById(R.id.description);
            img=(ImageView)myView.findViewById(R.id.micon);
            title.setText(mtitle[position]);
            discription.setText(mdiscription[position]);
            img.setImageResource(mimg[position]);
            return myView;
        }
    
    }
    

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

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
    
     private ListView myListView;
        Context context;
    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};
        public static String [] prgmNameList={"Let you C","c+++","JAVA/OAK","Jsp/Server App","Microsoft's.Net","Android The Best","PHP/Server","Jquery","JavaScript"};
        public static String [] prgmNameMyList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
    
    
    
    
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
     myListView=(ListView)findViewById(R.id.list_of_items);
    myListView.setAdapter(new MyAdapter(context,prgmNameList,prgmNameMyList,prgmImages));
    
    }}

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: