How to create custom array adapter in android?
Saved by
@Awais malik
#android
#java
Step 0: First of all you create an layout file in res->layout in which you design your desired layout of array in following code i am designing an array list with an icon,name an checkBox
<?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
Step 1: Now we create a class which extends BaseAdapter class and in this we set our layout for array this is basically our custom array adapter
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 array to our layout
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
Step 2: now in which activity we want to use this adapter we use it as follow
private AppAdapter mAppAdapter;
private ListView mProfileListView;
//suppose we have a list named array which we want to set using custom adapter we set it as follow
mAppAdapter = new AppAdapter(getApplicationContext(), array);
mProfileListView.setAdapter(mAppAdapter);
content_copyCOPY
In this tutorial i will teach you how to create a custom array adapter, array adapter is use to give an array list layout of your choice like icon, image , check box etc
Comments