列表视图未填充ListAdapter

时间:2015-04-27 19:13:22

标签: android listview android-listview adapter

我正在开发一款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方法...请帮助!!提前谢谢!

1 个答案:

答案 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会运行我猜。