0 works

How to create custom array adapter in android?


dashboard+ Project

Posted by @Awais malik #android #java

Step 0: first of all we design layout for our adapter i will give you an example code in which we design list in which we have an icon,name and a check box

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:focusable="false"
        android:clickable="false"
        android:contentDescription="@string/app_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:paddingBottom="8dp"/>

    <TextView
        android:id="@+id/list_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textIsSelectable="false"
        android:textSize="16dp"
        android:textColor="#000000"
        android:layout_toEndOf="@+id/app_icon"
        android:layout_alignTop="@+id/app_icon"
        android:layout_marginStart="16dp"
        android:layout_marginTop="2dp"
        android:text="@string/app_name"/>
    <CheckBox
        android:id="@+id/list_app_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="12dp"
        android:checked="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:layout_alignTop="@id/list_app_name"

        />

</RelativeLayout>

content_copyCopy to Clipboard

Step 1: now we create adapter in which we set this layout

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

import java.util.List;

public class AppAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    private List<AppList> listStorage;
   // boolean[] checkBoxState;

    public AppAdapter() {

    }

    public AppAdapter(Context context, List<AppList> customizedListView) {
        layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//here we set list which we want to display
        listStorage = customizedListView;
    }

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

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

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

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

        ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.installed_apps_list, parent, false);

            listViewHolder.textInListView = convertView.findViewById(R.id.list_app_name);
            listViewHolder.imageInListView = convertView.findViewById(R.id.app_icon);
            listViewHolder.checkBoxInListView = convertView.findViewById(R.id.list_app_checkbox);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();
            //for check boxes

            AppList userModel = listStorage.get(position);
            if(userModel.isSelected){
                listViewHolder.checkBoxInListView.setChecked(true);
            }else{
                listViewHolder.checkBoxInListView.setChecked(false);
            }
        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());



        return convertView;
    }

    void updateView(){
        notifyDataSetChanged();
    }
    static class ViewHolder{

        TextView textInListView;
        ImageView imageInListView;
        CheckBox checkBoxInListView;
    }

}

content_copyCopy to Clipboard

in this tutorial we will learn about how to create a custom adapter for an array list

,

>> Browse more code snippets

more_vert