我正在开发一款Android应用,其中ListView
位于由自定义DrawerLayout
填充的ListAdapter
中。 ListView
永远不会填充......我无法理解为什么......
以下是ListView的适配器设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
sideMenuLayout = (DrawerLayout) findViewById(R.id.drawer_base_activity_layout);
menuItemListView = (ListView) findViewById(R.id.sideMenuList);
//Pour les tests afin d'ignorer les certifications SSL
try {
Tools.skipSslCert();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
PsCategoriesServices categoriesServices = new PsCategoriesServices();
categoriesServices.getAllCategories(new OnCategoriesServicesListener(){
@Override
public void OnResponse(ArrayList<Categorie> listCategories) {
for(int catNb = 0; catNb < listCategories.size();catNb++)
{
if(listCategories.get(catNb).getNiveauHierarchique() == FIRST_CATEGORIES_ID)
{
menuItems.add(new SideMenuItem(listCategories.get(catNb)));
}
}
menuAdapter = new MenuListAdapter(getApplicationContext(), menuItems);
menuItemListView.setAdapter(menuAdapter);
}
@Override
public void OnError(VolleyError error) {
// TODO Auto-generated method stub
}
});
}
我从OnResponse
方法获取数据。
这是适配器:
public class MenuListAdapter extends ArrayAdapter<SideMenuItem> {
private Context mContext;
private ArrayList<SideMenuItem> navDrawerItems;
public MenuListAdapter(Context context, ArrayList<SideMenuItem> navDrawerItemsList){
super(context, 0, navDrawerItemsList);
mContext = context;
navDrawerItems = navDrawerItemsList;
}
@Override
public int getCount() {
return navDrawerItems.size();
}
@Override
public SideMenuItem getItem(int position) {
return navDrawerItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SideMenuItem menuItemtitle = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_item_layout, parent, false);
}
TextView menuItemTitle = (TextView) convertView.findViewById(R.id.menuItemLink);
menuItemTitle.setText(menuItemtitle.getTitle().toString());
return convertView;
}
}
我已经覆盖了Adapter中的所有方法,值不为null ......从不调用GetView方法...请帮助!!提前谢谢!
答案 0 :(得分:0)
试试这个getView()方法..它应该可以工作:
@override
public View getView(final int position, View convertView, ViewGroup
parent)
{
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.menu_item_layout,parent, false);
// do the code here...
return v;
}
至少你的getView()mehod会运行我猜。