How to make Adapters backward compatible?

To make Adapters backward compatible in Android, you can utilize the support libraries or AndroidX. This approach ensures that you're able to use newer features while still supporting older versions of Android. Below is an example of how to create a backward-compatible Adapter.

import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import androidx.annotation.NonNull; import java.util.List; public class MyAdapter extends ArrayAdapter { private Context context; private List items; public MyAdapter(@NonNull Context context, List items) { super(context, 0, items); this.context = context; this.items = items; } @NonNull @Override public View getView(int position, View convertView, @NonNull ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false); } TextView textView = convertView.findViewById(android.R.id.text1); textView.setText(items.get(position)); return convertView; } }

Adapter backward compatibility Android support libraries AndroidX