articles

Home / DeveloperSection / Articles / Adapter in Android

Adapter in Android

Manoj Pandey3082 30-May-2015

Adapters are the link between a set of data and the AdapterView that displays the data.

AdapterView :

AdapterViews are ViewGroups that display child views given to it by an adapter. An example of an AdapterView is a ListView.

Adapters get the data and pass it, along with a child view, to the parent AdapterView which displays the child view and the data.

Adapters call the getView() method which returns a view for each item within the adapter view. Get a View that displays the data at the specified position in the data set.

Adapter in Android

Some useful Adapters :
  •  ArrayAdapter
  •  CursorAdapter
  •  BaseAdapter
  •  ListAdapter
Example of ArrayAdapter :

My listview_item xml file

  

<?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="horizontal"

    android:paddingLeft="10sp"

    android:paddingRight="10sp"

    android:paddingTop="5sp" >

 

<ImageView

        android:layout_width="40sp"

        android:layout_height="40sp"

        android:src="@drawable/ic_launcher" />

 

    <TextView

        android:layout_marginLeft="10sp"

        android:layout_marginTop="9sp"

        android:id="@+id/myTextview"

        android:layout_width="wrap_content"

        android:textSize="20sp"

        android:layout_height="wrap_content"

        android:text="Sample Application" />

</LinearLayout>

 

MainActivity.class
  

public class MainActivity extends Activity {

     List<String> myList;

     ListView listview;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           listview=(ListView)findViewById(R.id.listview);

           myList = new ArrayList<String>();

           for (int i = 1; i < 21; i++)

                myList.add("Item " + i);

           MyAdapter adapter = new MyAdapter(getApplicationContext(),

                R.layout.listview_item, myList);

           listview.setAdapter(adapter);

}

}

 

This is my Adapter class
  

private class ViewHolder {

      TextView text;

           // ImageView imgIcon;

}

 

class MyAdapter extends ArrayAdapter<String> {

      Context myContext;

      LayoutInflater inflater;

      List<String> myList;

 

 public MyAdapter(Context context, int resource, List<String> objList) {

             super(context, resource, objList);

             // TODO Auto-generated constructor stub

             inflater = LayoutInflater.from(context);

             myList = objList;

             myContext = context;

    }

 

      @Override

public View getView(int position, View convertView, ViewGroup parent) {

             // TODO Auto-generated method stub

             final ViewHolder holder;

             if (convertView == null) {

                 holder = new ViewHolder();

          convertView = inflater.inflate(R.layout.listview_item, null);

                 // Locate the TextViews in listview_item.xml

                 holder.text = (TextView) convertView .findViewById(R.id.myTextview); 

                 convertView.setTag(holder);

             } else {

                 holder = (ViewHolder) convertView.getTag();

             }


             holder.text.setText(myList.get(position));

 

             return convertView;

      }

}

 

Example of baseAdapter

Simply add BaseAdapter instead of ArrayAdapter<String> or ArrayAdapter. It will be add 4 override method.

 

 

class MyAdapter extends BaseAdapter { 

      @Override

      public int getCount() {

           // TODO Auto-generated method stub

           return 0;

      }

 

      @Override

      public Object getItem(int position) {

           // TODO Auto-generated method stub

           return null;

      }

 

      @Override

      public long getItemId(int position) {

           // TODO Auto-generated method stub

           return 0;

     }

 

      @Override

public View getView(int position, View convertView, ViewGroup parent) {

           // TODO Auto-generated method stub             

           return null;

      }

}

Difference between ArrayAdapter and Base Adapter :

Base Adapter as the name suggests, is a base class for all the adapters. When you are extending the Base adapter class you need to implement all the methods like getcount( ), getid( ) etc.

ArrayAdapter is a class which can work with array of data. You need to override only getview method.

Base adapter is abstract class whereas array adapter and the list adapter are the concrete classes.

Common base class of common implementation for an Adapter that can be used in both ListView (by implementing the specialized ListAdapter interface} and Spinner (by implementing the specialized SpinnerAdapter interface.A concrete BaseAdapter that is backed by an array of arbitrary objects.


Updated 07-Sep-2019

Leave Comment

Comments

Liked By