WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Custom ArrayAdapter Android


ArrayAdapter is a bridge between UI component and data source that helps us to fill data in User Interface component.

ArrayAdapter is a an implementation of BaseAdapter.

Here the Android custom adapter code when extends ArrayAdapter

public class MyAdapter extends ArrayAdapter {

public MyAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
}

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

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

Custom ArrayAdapter Android Example

open android studio a new project and name it ArrayAdapterExample.
Select File -> New -> New Project -> and Fill the forms and click "Finish" button

Now open res -> layout -> xml (or) activity_main.xml


<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="#000"
        android:dividerHeight="5dp"/>

new layout in res-> layout and name it list_view_items.xml.


<?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="wrap_content"
    android:orientation="horizontal">

<ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp"
        android:src="@drawable/ic_launcher" />

<TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="Demo"
        android:textColor="#000" />
</LinearLayout>

open app – > java -> package -> MainActivity.java

package example.welookups.customarrayadapterexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView simpleList;
    ArrayList animalList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView) findViewById(R.id.simpleListView);
        fruitsList.add(new Item("Nectarine",R.drawable.Nectarine));
        fruitsList.add(new Item("Olives",R.drawable.Olives));
        fruitsList.add(new Item("Mulberries",R.drawable.Mulberries));
       fruitsList.add(new Item("Orange",R.drawable.Orange));
        fruitsList.add(new Item("Papaya",R.drawable.Papaya));
        fruitsList.add(new Item("Passion Fruit",R.drawable.Pear));

        MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,fruitsList);
        simpleList.setAdapter(myAdapter);
       }
}

Create a new Class app -> java ->package->MyAdapter.java

package example.welookups.customarrayadapterexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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

public class MyAdapter extends ArrayAdapter {

    ArrayList animalList = new ArrayList<>();

    public MyAdapter(Context context, int textViewResourceId, ArrayList objects) {
        super(context, textViewResourceId, objects);
        animalList = objects;
    }

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

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

        View v = convertView;
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_view_items, null);
        TextView textView = (TextView) v.findViewById(R.id.textView);
        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        textView.setText(animalList.get(position).getAnimalName());
        imageView.setImageResource(animalList.get(position).getAnimalImage());
        return v;

    }

}

a new Class src-> package-> Item.java

package example.welookups.customarrayadapterexample;
public class Item {

    String fruitsName;
    int fruitsImage;

    public Item(String fruitsName,int fruitsImage)
    {
        this.fruitsImage=fruitsImage;
        this.fruitsName=fruitsName;
    }
    public String getfruitsName()
    {
        return fruitsName;
    }
    public int getfruitsImage()
    {
        return fruitsImage;
    }
}