ArrayAdapter in Android

Overview

ArrayAdapter is used to create a customized item for Spinner.

Example of Simple ArrayAdapter

public class UserAdapter extends ArrayAdapter<User> {


public UserAdapter(Context context, ArrayList<User> centerList) {
super(context, 0, centerList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return initView(position, convertView, parent);
}

private View initView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.style_spinner_mnames, parent, false
);
}

TextView modelName = convertView.findViewById(R.id.modelName);


User currentUser = getItem(position);
if (currentUser != null) {
modelName.setText(currentUser.getName());

}
return convertView;
}
}

Reference



Comments

Popular posts from this blog

Architecture Components in Android

DataBinding in Android

SSLSocketFactory in Android