2013-07-26 74 views
4

我创造一个相对布局抽屉布局和列表视图中(为主要内容的一个,一个用于导航)的Android DrawerLayout和ListView与自定义适配器

为ListView我创建了一个自定义的适配器和我使用具有图像视图和文本视图的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); 

回答

1

要么使用:

super(context, layoutResourceId, categs);

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

@Override 
public int getCount() { 
    return data.length; 
} 
+0

我多么愚蠢的忘记了!谢谢 :) – JcDenton86

相关问题