4

我正在创建一个带有相对布局和内部列表视图的抽屉布局(一个用于主要内容,一个用于导航)

对于列表视图,我创建了一个自定义适配器,并使用具有 imageview 和 textview 的 list_item xml 文件创建每一行。

该应用程序运行,但是当我打开抽屉时,我只看到没有列表视图的背景。现在,如果我使用 ArrayAdapter(默认)尝试它,则会显示列表视图。

有什么建议吗?

我的自定义适配器

public class CustomAdapter extends ArrayAdapter<Categories>{

Context context;
int layoutResourceId;
Categories[] data = null;

public CustomAdapter(Context context, int layoutResourceId, Categories[] categs) {
    super(context, layoutResourceId);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    data = categs;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    PHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new PHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.categimage);
        holder.txtTitle = (TextView)row.findViewById(R.id.categtext);

        row.setTag(holder);
    }
    else
    {
        holder = (PHolder)row.getTag();
    }

    Categories categ = data[position];
    holder.txtTitle.setText(categ.title);
    holder.imgIcon.setImageResource(categ.icon);

    return row;
}

static class PHolder
{
    ImageView imgIcon;
    TextView txtTitle;
}

}

在我的主要活动中

mDrawerList = (ListView) findViewById(R.id.categlist);

    Categories data[] = new Categories[]
    {
        new Categories(R.drawable.restaurant, R.string.food),
        new Categories(R.drawable.bar_coktail, R.string.bar),
        new Categories(R.drawable.mall, R.string.shop),
        new Categories(R.drawable.agritourism, R.string.out),
        new Categories(R.drawable.dance_class, R.string.art),
        new Categories(R.drawable.officebuilding, R.string.other),
        new Categories(R.drawable.university, R.string.education),
        new Categories(R.drawable.townhouse, R.string.house),
        new Categories(R.drawable.junction, R.string.transport)
    };
    CustomAdapter ca = new CustomAdapter(this, R.layout.list_item, data);

    View header = (View)getLayoutInflater().inflate(R.layout.list_header, null);
    mDrawerList.addHeaderView(header);

    mDrawerList.setAdapter(ca);
4

1 回答 1

1

要么使用:

super(context, layoutResourceId, categs);

在构造函数中或覆盖getCount()方法:

@Override
public int getCount() {
   return data.length;
} 
于 2013-07-26T09:18:36.737 回答