WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Adapter Tutorial


Adapter View then view can takes the data from the adapter view .


Adapters In Android

Adapters In Android are following way to used:

SimpleAdapter It is an simple adapter to map static data to views defined in your XML file.
Custom ArrayAdapter used to display a custom list.
ArrayAdapter used whenever We have a list of single items which is backed by an array.
BaseAdapter used to parent adapter for all other adapters.

CustomAdapter

CustomAdapter is used to display a custom list. Two view ListView or a GridView we need to implement simple Adapter but when we need more customization in list or grid items where we have many view in a list item.

Custom Adapter extends SimpleAdapter


public class CustomAdapter extends SimpleAdapter {
public CustomAdapter(Context context, List> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);

}

@Override
public int getCount() {
return super.getCount();
}
}

SimpleAdapter In Android

SimpleAdapter is an achieved without great Adapter to map static data to views defined in an XML file. we can more customization in a ListView or a GridView we need to implement simple adapter. SimpleAdapter

SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

ArrayAdapter

we can a list of single items which is backed by an Array like phone contacts, countries or names


ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

BaseAdapter

It can be implementation of an Adapter that can be used in ListView, GridView, Spinner etc.

we customized list in a ListView or customized grids in a GridView we create our own adapter
public class CustomAdapter extends BaseAdapter {

@Override
public int getCount() {
return 0;
}

@Override
public Object getItem(int i) {
return null;
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

return null;
}

Adapter Example In Android

Create a new project and name it SimpletestAdapter Example.

Open res -> layout -> xml (or) main.xml and paste this code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#a0f"
android:dividerHeight="4dp"
android:listSelector="#200"/>



Save vegetables images in drawable folder with name cabbage, turnip, radish, carrot and cassava.

package com.faceadityasingh.com.myapplicationtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    //initialize view's
    ListView simpleListView;
    String[] fruitsNames = {"cabbage", "turnip", "turnip", " radish", "cassava"};//vegetables names array
    int[] fruitsImages = {R.drawable.cabbage, R.drawable.turnip, R.drawable. radish, R.drawable.carrot, R.drawable.cassavae};//vegetablesimages
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleListView=(ListView)findViewById(R.id.simpleListView);

        ArrayList> arrayList=new ArrayList<>();
        for (int i=0;i hashMap=new HashMap<>();//create a hashmap to store the data in key value pair
            hashMap.put("name",fruitsNames[i]);
            hashMap.put("image",fruitsImages[i]+"");
            arrayList.add(hashMap);//add the hashmap into arrayList
        }
        String[] from={"name","image"};//string array
        int[] to={R.id.textView,R.id.imageView};//int array of views id's
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.list_view_items,from,to);//Create object and set the parameters for simpleAdapter
        simpleListView.setAdapter(simpleAdapter);//sets the adapter for listView

        //perform listView item click event
        simpleListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),fruitsNames[i],Toast.LENGTH_LONG).show();//show the selected image in toast according to position
            }
        });
    }

    
}

Create new layout-> rec-> layout-> list_view_items.xml and set this code.

<?xml version="1.0" encoding="utf-8"?>


<ImageView
android:id="@+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="6dp"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:text="test only"
android:textColor="#af0" />
</RelativeLayout>