user3108740 user3108740 - 4 months ago 38
Android Question

getview is not called in BaseAdapter

I have a problem with my custom adapter. the method getView() is not called. I searched on my friends "google" and "stackoverflow" but anything fix my problem.

public class custom_adapter extends BaseAdapter {
private List<Activities> listData;

private LayoutInflater layoutInflater;

public custom_adapter(Context context,
List<Activities> listActivity) {
this.listData = listActivity;
layoutInflater = layoutInflater.from(context);
Log.v("BaseAdapter", "custom_adapter");
}


@Override
public int getCount() {
Log.v("BaseAdapter - getCount", String.valueOf(listData.size()));
return listData.size();
}

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

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("BaseAdapter", "getView");
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Activities newsItem = (Activities) listData.get(position);

holder.name.setText(newsItem.getName());
holder.ratingBar.setRating(newsItem.getAverageMark());

if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
}

return convertView;
}


public static class ViewHolder {
TextView name;
RatingBar ratingBar;
ImageView imageView;
}


}


Furthermore, GetCount is not null !

I will be very glade if you can help me

Regards

K.L

Answer

For the purpose of handling List of some objects, extending generic ArrayAdapter may be better choice instead of just BaseAdapter. It will handle a lot of things for you. Your whole adapter class could look like.

public class ActivitiesAdapter extends ArrayAdapter<Activities>
{
private LayoutInflater layoutInflater;

public ActivitiesAdapter(Context context, List<Activities> objects)
{
    super(context, 0, objects);

    layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.v("BaseAdapter", "getView");
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.custom_adapter, null);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.title);
        holder.ratingBar = (RatingBar) convertView.findViewById(R.id.averageRatingBarActivitySearch);
        holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Activities newsItem = getItem(position);

    holder.name.setText(newsItem.getName());
    holder.ratingBar.setRating(newsItem.getAverageMark());

    if (holder.imageView != null) {
        new ImageDownloaderTask(holder.imageView).execute(newsItem.getPictureActivityString());
    }

    return convertView;
}


public static class ViewHolder {
    TextView name;
    RatingBar ratingBar;
    ImageView imageView;
}
}

and of course you have to set this adapter to your ListView instance. Like

ActivitiesAdapter adapter = new ActivitiesAdapter(this, yourActivitiesList);
ListView listView = (ListView)findViewById(R.id.my_list_id);
listView.setAdapter(adapter);

or simply setListAdapter(adapter); if you are using ListActivity or ListFragment;

Comments