1

我需要一个 gridview 来显示 9 个项目。我写了一个自定义的baseadapter。

但是,我对getView方法中的位置有疑问。看起来这个 gridview 错过了第 7 项。代码如下所示:

public class ImageAdapter extends BaseAdapter {
private Hashtable<String, LayoutDTO> menuHashtable =  Global.menuHashtable;
private LayoutInflater mInflater;

public ImageAdapter(Context context) {
    this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    //Log.v("size........", Integer.toString(menuHashtable.size()));
    return menuHashtable.size();
}

@Override
public Object getItem(int position) {
    return menuHashtable.get(Integer.toString(position));
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutDTO lDto = menuHashtable.get(Integer.toString(position));
    ViewHolder vHolder = new ViewHolder();
    //if (convertView == null) {
        if (lDto != null) {
            String titleString = lDto.getTitle();
            Bitmap iconBitmap = lDto.getIcon();
            convertView = mInflater.inflate(R.layout.custombutton, null);
            vHolder.icon = (ImageView) convertView
                    .findViewById(R.id.imageicon);
            vHolder.icon.setImageBitmap(iconBitmap);
            vHolder.text = (TextView) convertView
                    .findViewById(R.id.icontitle);
            int index = titleString.indexOf("\u0026");
            if (index != -1) {
                String title1 = titleString.substring(0, index + 1).trim();
                String title2 = titleString.substring(index + 1,
                        titleString.length()).trim();
                vHolder.text.setLines(2);
                String newtitle = title1 + "\n" + title2;
                vHolder.text.setText(newtitle);
            } else {
                vHolder.text.setLines(2);
                String newtitle = titleString + "\n" + " ";
                vHolder.text.setText(newtitle);
            }
            convertView.setTag(vHolder);
        }
        else{
            Log.v("null........",Integer.toString(position));
        }

    return convertView;
}

private class ViewHolder {
    ImageView icon;
    TextView text;
}

}

日志显示:

05-20 21:37:16.066: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.105: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.125: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.135: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.166: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.185: VERBOSE/title...........(158): Entertainment  1
05-20 21:37:16.195: VERBOSE/title...........(158): Shopping  2
05-20 21:37:16.195: VERBOSE/title...........(158): Fashion  3
05-20 21:37:16.205: VERBOSE/title...........(158): Health & Beauty  4
05-20 21:37:16.215: VERBOSE/title...........(158): Supermarkets  5
05-20 21:37:16.226: VERBOSE/title...........(158): Auto Services  6
05-20 21:37:16.236: VERBOSE/title...........(158): Travel & Accommodation  8
05-20 21:37:16.316: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.326: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.336: VERBOSE/title...........(158): Dining  0
05-20 21:37:16.345: VERBOSE/title...........(158): Dining  0
4

0 回答 0