Search code examples
androidandroid-listadapter

How to implement a custom adapter with listview?


Hi I'm working with an android app when I need to parse an array in the list view. I have created a custom adapter for that but I'm missing something here.

public class IndustryAdapter extends ArrayAdapter<Industry> {
    Context context;
    int resource;
    public IndustryAdapter(Context context, ArrayList<Industry> industries) {
        super(context, industries);
    }

    @Override
    public View getView(View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
        TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);

        return convertView;
    }


}

On the super(context, industries) is showing an error for industries:

required: 'int'
error: incompatible types: ArrayList<Industry> cannot be converted to int

Solution

  • You should just change your super constructor invoke. Because it receive the layout resource which you gonna inflate. Like this:

    public IndustryAdapter(Context context, ArrayList<Industry> industries) {
                super(context, android.R.layout.simple_list_item_1);
            }