使用GetResources向NavigationDrawer ListView添加多个项目

时间:2014-10-29 16:19:46

标签: android android-listview navigation-drawer

我有一个非常有趣的错误。我使用getResources()。getString()创建了许多列表项。然而, 我的上一个listview项目显示了listview中的第二个项目。我还尝试将最后一项添加为常量字符串,但它仍然将最后一项显示为第二项。可能是一个与我相关的错误我使用getResources向listview添加了太多项目?你知道这里发生了什么吗?

NavSettingsObject[] navSettingsObjects = new NavSettingsObject[] {
                // First Section
                NavSettingsSection.create(res.getString(
                        R.string.s1)),
                // Second Section
                NavSettingsSection.create(res.getString(
                        R.string.s2)),
                NavSettingsItem.create(res.getString(
                        R.string.i1)),
                NavSettingsItem.create(res.getString(
                        R.string.i2)),
                NavSettingsItem.create(res.getString(
                        R.string.i3)),
                NavSettingsItem.create(res.getString(
                        R.string.i4)),
                NavSettingsItem.create(res.getString(
                        R.string.i5)),
                NavSettingsItem.create(res.getString(
                        R.string.i6)),
                NavSettingsItem.create(res.getString(
                        R.string.,7)),
                NavSettingsItem.create(res.getString(
                        R.string.i8)),
                NavSettingsItem.create(res.getString(
                        R.string.i9)),
                NavSettingsItem.create(res.getString(
                        R.string.i10)),
                // Third Section
                NavSettingsSection.create(res.getString(
                        R.string.s3)),
                NavSettingsItem.create(res.getString(
                        R.string.i11)),
                NavSettingsItem.create(res.getString(
                        R.string.i12)),
                NavSettingsItem.create(res.getString(
                        R.string.i13)),
                NavSettingsItem.create(res.getString(
                        R.string.i14)),
                NavSettingsItem.create(res.getString(
                        R.string.i15))};
     //Fourth Section
    //**********************************************HERE COMES THE BUG**************************
   //Adding one more section item with different string, it displays the second item which is R.string.s2
                NavSettingsItem.create(res.getString(
                            R.string.i16))

        settingsAdapter = new NavSettingsListAdapter(getApplicationContext(),
                R.layout.settings_item, navSettingsObjects);
        mDrawerSettingsList.setAdapter(settingsAdapter);

更新:NavSettingsListAdapter.java

public class NavSettingsListAdapter extends ArrayAdapter<NavSettingsObject> {
private LayoutInflater inflater;

    public NavSettingsListAdapter(Context context, int textViewResourceId, NavSettingsObject[] objects ) {
        super(context, textViewResourceId, objects);
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null ;
        NavSettingsObject menuItem = this.getItem(position);
        if ( menuItem.getType() == NavSettingsItem.ITEM_TYPE ) {
            view = getItemView(convertView, parent, menuItem );
        }
        else {
            view = getSectionView(convertView, parent, menuItem);
        }
        return view ;
    }

    public View getItemView( View convertView, ViewGroup parentView, NavSettingsObject navDrawerItem ) {

        NavSettingsItem menuItem = (NavSettingsItem) navDrawerItem ;

        if (convertView == null) {
            convertView = inflater.inflate( R.layout.settings_item, parentView, false);
            TextView labelView = (TextView) convertView
                    .findViewById( R.id.item_label );
            labelView.setText(menuItem.getLabel());
        }

        return convertView ;
    }

    public View getSectionView(View convertView, ViewGroup parentView,
            NavSettingsObject navDrawerItem) {

        NavSettingsSection menuSection = (NavSettingsSection) navDrawerItem ;

        if (convertView == null) {
            convertView = inflater.inflate( R.layout.settings_section, parentView, false);
            TextView labelView = (TextView) convertView
                    .findViewById( R.id.section_label );
            labelView.setText(menuSection.getLabel());
        }

        return convertView ;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return this.getItem(position).getType();
    }

    @Override
    public boolean isEnabled(int position) {
        return getItem(position).isEnabled();
    }
}  

0 个答案:

没有答案