简体   繁体   中英

Android DrawerLayout and Listview with custom adapter

I am creating a drawer layout with a relative layout and a listview inside (one for the main content and one for the navigation)

For the listview i created a custom adapter and i create each row using a list_item xml file which has an imageview and a textview.

The app runs but when i open the drawer i see only the background without the listview. Now if i try it with the ArrayAdapter (default) the listview shows up.

Any advice?

My custom adapter

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;
}

}

And in my main activity

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);

Either use:

super(context, layoutResourceId, categs);

in the constructor or override the getCount() method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM