Android DrawerLayout and Listview with custom adapter

I am creating a box layout with a relative location and a list inside (one for main content and one for navigation)

For the list, I created a custom adapter, and I create each line using the XML file list_item, which has an image and text representation.

The application starts, but when I open the box, I see only the background without a list. Now, if I try it with the ArrayAdapter (by default), a list will appear.

Any tips?

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); 
+4
source share
1 answer

Or use:

super(context, layoutResourceId, categs);

in the constructor or override the getCount() method:

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

Source: https://habr.com/ru/post/1493616/


All Articles