开发者

Custom Adapter for a Gallery

I've come across a problem while writing a custom Adapter for the items on a Gallery. I followed the steps on the android developer site, but instead of returning an ImageView I'm returning a TextView. However, when I try to run the application I get a ClassCastException when trying to add child items to the Gallery.

However if I use a basic string adapter for the gallery it works, an开发者_Python百科d if I use ImageView it works too. I don't get the problem since the method

@Override
public View getView(int position, View convertView, ViewGroup parent)

return a View which is the base class for both ImageView and TextView. So I don't what could be possible wrong. Do you know what could be the cause of the problem?

Thanks a lot

This is the code of the adapter,

public class HistoryIndexAdapter extends BaseAdapter {

private Context context;
private ArrayList<History> historyIndex;
private Typeface typeface;

public HistoryIndexAdapter(Context context, ArrayList<History> historyItems) {

    this.context = context;
    this.historyIndex = historyItems;    
    this.typeface = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.font));
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    TextView item = new TextView(context);


    item.setText(historyIndex.get(position).getDate());
    item.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    item.setTypeface(typeface);
    item.setTextColor(R.color.white);

    return item;
}

}


What is the fully qualified LayoutParams class you are using? You need to use Gallery.LayoutParams. If you don't, you will get a ClassCastException.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜