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);
我多麼愚蠢的忘記了!謝謝 :) – JcDenton86