对SimpleCursorAdapter进行子类化以包含convertView以进行内存保护

Sha*_*awn 4 android list recycle android-listview

我一直在搜索示例和教程,但我似乎无法理解如何在子类SimpleCursorAdapter中处理回收.我知道对于常规ArrayAdapter,你可以检查convertView for null和inflate,如果null是xml,如果不是null,则回收,但是我在查看SimpleCursorAdapter子类中from和to数组的工作方式时遇到了一些麻烦.我试图通过Commonsware的"繁忙的Android开发编码器指南"来解决这个问题,但是没有成功.如果有人知道任何提示,示例或教程,我将很高兴看到它们.

Sha*_*awn 8

我无法找到任何优秀的例子来简单地使用SimpleCursorAdapter来使用convertView循环方法,所以我最终继承了CursorAdapter的子类.这实际上对我的实现非常有效,并且随着内存使用量的急剧下降而快速发展.回收意见确实有效,我推荐它!

以下是我的实施示例:

 private static class MyNiftyAdapter extends CursorAdapter
    {
        private LayoutInflater mInflater;
        private Cursor cur;

        public MyNiftyAdapter(Context context, Cursor c) {
            super(context,c);       
            this.mInflater = LayoutInflater.from(context);
            this.cur = c;
        }
        public MyNiftyAdapter(Context context, Cursor c, boolean autoRequery)
        {
            super(context, c, autoRequery);
            this.mInflater = LayoutInflater.from(context);
            this.cur = c;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder viewHolder;
            if(convertView == null)
            {
                convertView = this.mInflater.inflate(R.layout.chamber_item, null);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView)convertView.findViewById(R.id.Name);
                viewHolder.city = (TextView)convertView.findViewById(R.id.city);
                viewHolder.state = (TextView)convertView.findViewById(R.id.state);
                viewHolder.country = (TextView)convertView.findViewById(R.id.country);
                convertView.setTag(viewHolder);
            }else
            {
                viewHolder = (ViewHolder)convertView.getTag();
            }
            this.cur.moveToPosition(position);

            viewHolder.name.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.NAME)));          
            viewHolder.city.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.CITY)));
            viewHolder.state.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.STATE)));
            viewHolder.country.setText(this.cur.getString(this.cur.getColumnIndex(MyDBHelper.COUNTRY)));            

            return convertView;
        }
        /* (non-Javadoc)
         * @see android.widget.CursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
         */
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Dont need to do anything here

        }
        /* (non-Javadoc)
         * @see android.widget.CursorAdapter#newView(android.content.Context, android.database.Cursor, android.view.ViewGroup)
         */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Dont need to do anything here either
            return null;
        }

        static class ViewHolder
        {
            TextView name;
            TextView city;
            TextView state;
            TextView country;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我列表中的每一行现在都按照我的要求显示名称,城市,州和国家/地区.希望这可以帮助.

  • 我认为你做的不是正确的方式.您可以简单地覆盖`newView()`和`bindView()`函数来创建新视图或使用以前创建的视图,而不是覆盖不是抽象方法的`getView()`.CursorAdapter将在必要时负责创建新视图或回收以前创建的视图.更少的代码,更清洁,实际上是standrad方式! (2认同)

Rom*_*Guy 5

CursorAdapter部分为您完成工作.您只需要在需要创建新视图时覆盖newView(),并在回收现有视图时覆盖bindView().