Advertisements

Simplest Way to Implement Custom Adaptor with List View|Android Studio

List View is used view in android studio. To implement a Custom Adapter on list view is really simple task just follow these steps. But First Understand why a custom adapter is required.

  1. To Impose/Inflate a custom view on the list view
  2. To set data in list view in each row which contains multiple elements like ImageView/ TextView/ EditText Etc.

Let’s start:

Create A Project go to activity_main.xml and create a list view.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ListView
       android:id="@+id/listview"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></ListView>

</android.support.constraint.ConstraintLayout>

Now Create A New Resource Layout File demolistview.xml and write code for designing of each row in Listview:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="16dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:padding="5dp"
        android:id="@+id/txt_countryname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

    <TextView
        android:padding="5dp"
        android:layout_alignParentEnd="true"
        android:id="@+id/txt_countrycode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints" />

</RelativeLayout>

Create A New Java Class Which is A POJO class or say A getter-setter class CountryPojo.java

public class CountryPojo {
    public String getCountryname() {
        return countryname;
    }

    public void setCountryname(String countryname) {
        this.countryname = countryname;
    }

    public String getCountrycode() {
        return countrycode;
    }

    public void setCountrycode(String countrycode) {
        this.countrycode = countrycode;
    }

    String countryname,countrycode;
}

Create a Custom Adapter Java class for binding list view and data CustomAdapter.java and write:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class CountryAdapter extends BaseAdapter {
    Context context;
    CountryPojo countryPojo=new CountryPojo();
    ArrayList<CountryPojo> mylist=new ArrayList<>();
    LayoutInflater layoutInflater;
    public CountryAdapter(Context context, ArrayList<CountryPojo> mylist){
        this.context=context;
        this.mylist=mylist;
        layoutInflater=LayoutInflater.from(this.context);
    }
    @Override
    public int getCount() {
        return mylist.size();
    }

    @Override
    public CountryPojo getItem(int position) {
        return mylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;

        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.demolistview,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else{
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        countryPojo = getItem(position);
        myViewHolder.txt_countrycode.setText(countryPojo.getCountrycode());
        myViewHolder.txt_country.setText(countryPojo.getCountryname());
        return convertView;
    }
    public class MyViewHolder{
        TextView txt_country,txt_countrycode;
        ImageView img;

        public MyViewHolder(View view){
            txt_country=view.findViewById(R.id.txt_countryname);
            txt_countrycode=view.findViewById(R.id.txt_countrycode);

        }
    }
}

Go to your MainActivity.java and write:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<CountryPojo> mylist;
CountryPojo countryPojo;
CountryAdapter countryAdapter;
String country[]={"India","US","UK","Africa","Russia","China","India","US","UK","Africa","Russia","China"};
String countrycode[]={"+91","+92","+93","+11","+90","+34","+91","+92","+93","+11","+90","+34"};
int r=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.listview);
        mylist=new ArrayList<>();
        countryPojo=new CountryPojo();
        while (r<=10){
            countryPojo.setCountryname(country[r]);
            countryPojo.setCountrycode(countrycode[r]);
            mylist.add(countryPojo);
            countryPojo=new CountryPojo();
            r++;
        }
        countryAdapter=new CountryAdapter(MainActivity.this,mylist);
        listView.setAdapter(countryAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("Safiya", String.valueOf(listView.getItemAtPosition(position)));
               // Toast.makeText(MainActivity.this,  listView.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
            }

        });


    }
}

That’s it Now Run your Application. Watch Video to understand this code and also learn how to add SQL server connection to your android application.

Possible Problems you might face is just check your Package name, Import Files Correctly and cross check file names and you are good to go.

subscribe to My Newsletter.

Comment for any issue and subscribe to my YouTube channel

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Advertisements

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

Your Views And Question are Welcome Here!

Advertisements