Engineer Eraldo
Engineer Eraldo

Reputation: 13

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

Upvotes: 0

Views: 2166

Answers (2)

Rajasekaran M
Rajasekaran M

Reputation: 2538

Use base adapter for creating custom adapter for your list

class CustomAdapter extends BaseAdapter {
   private ArrayList<Industry> industries;
    private Context context;

    public CustomAdapter(ArrayList<Industry> industries, Context context) {
        this.industries = industries;
        this.context = context;
    }

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

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

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

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

        return convertView;
    }
}

Upvotes: 1

samaromku
samaromku

Reputation: 559

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);
        }

Upvotes: 0

Related Questions