在listitem上点击崩溃

时间:2014-07-02 05:14:09

标签: android listview navigation-drawer baseadapter onitemclicklistener

我有一个带有自定义列表视图的导航抽屉,我想这样做,以便在单击某个项目时所选项目的文本会发生变化。 我遇到的问题是listview没有被重新创建,textcolor没有被更改,即使我调用notifydatasetchanged()。我不知道问题是什么。谢谢:))

OnItemClickListener:

    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            getSupportFragmentManager().beginTransaction().replace(R.id.content_frame_main, new CampusLifeFragment()).commit();
            getActionBar().setTitle(mDrawerItems[i]);
            listAdapter = new DrawerListAdapter(LayoutInflater.from(this));
            listadapter.setSelectedItem(i);
            listAdapter.notifyDataSetChanged();
            mDrawerLayout.closeDrawer(mDrawerList);
        }

DrawerListAdapter:

public class DrawerListAdapter extends BaseAdapter {

        private LayoutInflater inflater;
        private int mSelectedItem;
        private int[] mDrawerDrawables = {R.drawable.ic_settings, R.drawable.ic_about};

        public DrawerListAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        public int getSelectedItem() {
            return mSelectedItem;
        }

        public void setSelectedItem(int i) {
            mSelectedItem = i;
        }

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

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (position <= 3) {
                convertView = inflater.inflate(R.layout.layout_drawer_primary, null);
                TextView titlePrimary = (TextView) convertView.findViewById(R.id.textView_drawer_list_primary);
                titlePrimary.setText(mDrawerItems[position]);
                titlePrimary.setTextColor((mSelectedItem == position) ? Color.parseColor("#3f51b5") : Color.parseColor("#454545"));

                return convertView;
            } else {
                    convertView = inflater.inflate(R.layout.layout_drawer_secondary, null);
                    TextView titlesecondary = (TextView) convertView.findViewById(R.id.textView_drawer_list_secondary);
                    titlesecondary.setText(mDrawerItems[position]);

                    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView_drawer_list_secondary);
                    imageView.setImageResource(mDrawerDrawables[position - 4]);

                    return convertView;
                }

            }
        }

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:windowContentOverlay">@drawable/drawer_shadow_horizontal</item>
        <item name="android:actionBarStyle">@style/actionBarStyle</item>

    </style>

    <style name="actionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:displayOptions">showTitle</item>
        <item name="android:background">@drawable/ab_back</item>
        <item name="android:textSize">20dp</item>
    </style>
</resources>

logcat的

07-02 01:20:33.701      676-676/com.prototype.ryersonapp D/dalvikvm﹕ GC_FOR_ALLOC freed 71K, 11% free 2932K/3292K, paused 10ms, total 10ms
07-02 01:20:33.781      676-676/com.prototype.ryersonapp D/dalvikvm﹕ GC_FOR_ALLOC freed 26K, 9% free 3412K/3720K, paused 13ms, total 13ms
07-02 01:20:33.811      676-676/com.prototype.ryersonapp D/dalvikvm﹕ GC_FOR_ALLOC freed 39K, 8% free 3724K/4044K, paused 9ms, total 9ms
07-02 01:20:33.811      676-676/com.prototype.ryersonapp I/dalvikvm-heap﹕ Grow heap (frag case) to 6.324MB for 2536936-byte allocation
07-02 01:20:33.821      676-707/com.prototype.ryersonapp D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 6201K/6524K, paused 8ms, total 8ms
07-02 01:20:34.111      676-676/com.prototype.ryersonapp I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
07-02 01:20:34.151      676-676/com.prototype.ryersonapp D/OpenGLRenderer﹕ Enabling debug mode 0

1 个答案:

答案 0 :(得分:0)

如果您只想更改颜色,请将listAdapter保存为成员变量,如

protected void onCreate(Bundle savedInstanceState) {
    mListAdapter = new DrawerListAdapter(LayoutInflater.from(this));
}

然后

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame_main, new CampusLifeFragment()).commit();
    getActionBar().setTitle(mDrawerItems[i]);
    mListAdapter.setItemChecked(i);
    mListAdapter.notifyDataSetChanged();
    mDrawerLayout.closeDrawer(mDrawerList);
}

或者如果你想制作一个新的适配器,

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame_main, new CampusLifeFragment()).commit();
    getActionBar().setTitle(mDrawerItems[i]);
    DrawerListAdapter listAdapter = new DrawerListAdapter(LayoutInflater.from(this));
    mDrawerLayout.closeDrawer(mDrawerList);
    mDrawerList.setAdapter(listAdapter);
    listAdapter.setItemChecked(i);
    listAdapter.notifyDataSetChanged();
}