Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

André Gomes
3,537 PointsBaseAdapter does not work
I decided to try the to make my own list adapter, I came up with the following code, but the app crashes when opened.
What is wrong?
Thanks in advance!
public class ListAdapter extends BaseAdapter {
private int [] SexImgArray = {
R.drawable.sex1,
R.drawable.sex2,
R.drawable.sex3,
R.drawable.sex4,
R.drawable.sex5,
R.drawable.sex6,
R.drawable.sex7,
R.drawable.sex8,
R.drawable.sex9,
R.drawable.sex10,
R.drawable.sex11,
R.drawable.sex12,
R.drawable.sex13,
R.drawable.sex14,
R.drawable.sex15,
R.drawable.sex16,
R.drawable.sex17,
R.drawable.sex18,
};
private String [] SexTitleArray = {
"Erotic V", //1
"Fantastic Rocking Horse", //2
"Catherine Wheel ", //3
"Glowing Triangle", //4
"X-Rated", //5
"Nirvana", //6
"Padlock", //7
"Slide", //8
"Ape", //9
"Butterfly", //10
"Ascent to Desire", //11
"Balancing Act", //12
"Bridge", //13
"Clip", //14
"Double Decker", //15
"Seduction", //16
"Crouching Tiger", //17
"Hero" //18
};
Context mContext;
public ListAdapter(Context context)
{
mContext = context;
}
@Override
public int getCount() {
return SexTitleArray.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.ImgView = (ImageView) convertView.findViewById(R.id.ImageView);
holder.TextView = (TextView) convertView.findViewById(R.id.TextView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.ImgView.setImageResource(SexImgArray[position]);
holder.TextView.setText(SexTitleArray[position]);
return convertView;
}
private static class ViewHolder {
ImageView ImgView; // public by default
TextView TextView;
}
}
1 Answer

Daniel Hartin
18,106 PointsHi Andre
I have been through your code as I did a very similar class for an app of mine and your code seems to match almost identically, the only real difference being my ViewHolder class is defined only as static where yours is private static so i wonder whether the inner class is being accessed from outside the outer class somewhere by android (only speculation i'm unsure).
What does your LogCat say?
I've attached my working example below just in case you can spot something else
package com.daniel.hartin.mysteryauditor.CustomListViews;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.daniel.hartin.mysteryauditor.ObjectHolders.Quota;
import com.daniel.hartin.mysteryauditor.R;
import com.daniel.hartin.mysteryauditor.Utilities.QuotaEditor;
import java.util.ArrayList;
public class CustomQuotaView extends BaseAdapter{
private static ArrayList<Quota> mQuotaList;
private LayoutInflater mInflater;
private Context mContext;
public CustomQuotaView(Context context) {
mContext = context;
QuotaEditor editor = new QuotaEditor(context);
mQuotaList = editor.getQuotaList();
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return mQuotaList.size();
}
public Object getItem(int position) {
return mQuotaList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_quota_view, null);
holder = new ViewHolder();
holder.serviceName = (TextView) convertView.findViewById(R.id.serviceName);
holder.totals = (TextView) convertView.findViewById(R.id.totalsValue);
holder.image = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.serviceName.setText(mQuotaList.get(position).getService());
holder.totals.setText(mQuotaList.get(position).getCompleted() + "/" + mQuotaList.get(position).getTotal());
if(mQuotaList.get(position).getCompleted() < mQuotaList.get(position).getTotal()) {
holder.image.setImageResource(R.drawable.red_cross);
}else {
holder.image.setImageResource(R.drawable.green_tick);
}
return convertView;
}
static class ViewHolder {
TextView serviceName;
TextView totals;
ImageView image;
}
}
Hope this is of some help
Daniel
André Gomes
3,537 PointsAndré Gomes
3,537 PointsThank you so much for your help, apricieted it very much. but the error I just found out now that since I am extending the activity from wich I will call the adapter ListActivity I need to set the id of the ListView to @android:id/list.
Thank anyway!