ResourceCursorAdapter中的多个Listview行选择

时间:2013-12-06 05:26:58

标签: android android-listview android-fragments contextual-action-bar

开发一个短信应用程序,其中UI设置是这样的。消息片段用于将每个消息线程显示为对话列表,ResoruceCursorAdapter用于将对话绑定到列表视图。取决于消息线程i将创建片段并将此片段加载到viewpager。所以我可以将每个消息线程作为片段滑动。现在问题。我想添加上下文操作栏以从列表视图中选择多个消息。这里面临的问题是它没有突出显示正确的列表行。当时我选择列表视图中的项目,只选择最后一个片段中的列表视图

 public class MessageAdapter extends ResourceCursorAdapter
    {

        private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();


        public MessageAdapter( Context _context, final Uri uri, int layout )
        {


            super( _context, layout, getCursor( _context.getContentResolver(), uri ), true );
            Log.e( "first uri in adapter is ", "" + uri.toString() );
            origCursor = getCursor( _context.getContentResolver(), uri );
            this.queryHandler = new BackgroundQueryHandler( _context.getContentResolver() );

            this.defaultContactAvatar = _context.getResources().getDrawable( R.drawable.default_avatar );

            this.textSize = PreferencesActivity.getTextsize( _context );
            this.textColor = PreferencesActivity.getTextcolor( _context );
            this.convertNCR = PreferencesActivity.decodeDecimalNCR( _context );

            context = _context;

        }

        public void setNewSelection(int position, boolean value) {
            mSelection.put(position, value);
            notifyDataSetChanged();
        }

        public boolean isPositionChecked(int position) {
            Boolean result = mSelection.get(position);
            return result == null ? false : result;
        }

        public Set<Integer> getCurrentCheckedPosition() {
            return mSelection.keySet();
        }

        public void removeSelection(int position) {
            mSelection.remove(position);
            notifyDataSetChanged();
        }

        public void clearSelection() {
            mSelection = new HashMap<Integer, Boolean>();
            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views

            Log.e("getCount()===========",""+getCount());
            if ( mSelection.get( position ) != null )
            {

                v.setBackgroundColor( context.getResources().getColor( android.R.color.holo_blue_light ) );
                LinearLayout sa = ( LinearLayout ) v.findViewById( R.id.messageBody );
                sa.setBackgroundColor( context.getResources().getColor( android.R.color.holo_blue_light ) );



            }
            else
            {

                v.setBackgroundColor( context.getResources().getColor( android.R.color.transparent ) );

                LinearLayout sa = ( LinearLayout ) v.findViewById( R.id.messageBody );
                sa.setBackgroundResource( R.drawable.classicselectorreceived );

            }


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



        @Override
        public View newView( Context context, Cursor cursor, ViewGroup parent )
        {

            LayoutInflater mInflater = ( LayoutInflater ) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

            rowType = getItemViewType( cursor.getPosition() );

            if ( rowType == 0 )
            {

                if ( PreferencesActivity.loadDefaultLayout( context ) )
                {
                    return mInflater.inflate( R.layout.drawermessage_classic_sent, parent, false );
                }
                else
                {
                    return mInflater.inflate( R.layout.testmessage_classic_sent, parent, false );
                }

            }
            else if ( rowType == 1 )
            {

                if ( PreferencesActivity.loadDefaultLayout( context ) )
                {
                    return mInflater.inflate( R.layout.drawermessage_classic_received, parent, false );
                }
                else
                {
                    return mInflater.inflate( R.layout.testmessage_classic_received, parent, false );
                }


            }
            else
            {
                return null;
            }

        }

        @Override
        public int getItemViewType( int position )
        {
            Cursor c = ( Cursor ) getItem( position );
            Message m = Message.getMessage( context, c );
            switch ( m.getType() )
            {
            case Message.MMS_IN: // 128
                return 1;
            case Message.MMS_OUT: // 132
                return 0;
            case Message.SMS_IN: // 2
                return 1;
            case Message.SMS_OUT: // 1
                return 0;
            default:
                return 0;
            }
        }


        public void setImageView( ImageView contactPhoto )
        {

            if ( android.os.Build.VERSION.SDK_INT >= 14 )
            {
                try
                {

                    mProfileCursor = context.getContentResolver()
                            .query( Profile.CONTENT_URI, mProjection, null, null, null );

                    try
                    {
                        if ( mProfileCursor.moveToFirst() )
                        {
                            do
                            {

                                sssssss = mProfileCursor.getString( mProfileCursor.getColumnIndex( Profile.PHOTO_URI ) );
                                if ( sssssss != null )
                                {
                                    Uri photoUri = Uri.parse( sssssss );
                                    contactPhoto.setImageURI( photoUri );
                                }
                                else
                                {
                                    contactPhoto.setImageResource( R.drawable.default_avatar );
                                }

                            } while ( mProfileCursor.moveToNext() );
                        }

                    }
                    catch ( Exception e )
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        mProfileCursor.close();
                        Log.d( "CURSOR CLOSED IN FINALLY", "CURSOR CLOSED IN FINALLY" );
                       }
                }

                catch ( Exception e )
                {
                    e.printStackTrace();
                    contactPhoto.setImageResource( R.drawable.default_avatar );
                }
            }
            else
            {
                contactPhoto.setImageResource( R.drawable.default_avatar );
            }
        }


        @Override
        public void bindView( View view, final Context context, Cursor cursor )
        {

            int viewType = getItemViewType( cursor.getPosition() );

            switch ( viewType )
            {
            case 0:

                final Message m = Message.getMessage( context, cursor );
                holder = ( ViewHolder ) view.getTag();
                if ( holder == null )
                {

                    holder = new ViewHolder();
                    holder.tvBody = ( TextView ) view.findViewById( R.id.textBody );
                    holder.tvDate = ( TextView ) view.findViewById( R.id.textDate );
                    // holder.vRead= (View) view.findViewById(R.id.read);
                    Utilities.setCustomFont( context, holder.tvDate );
                    Utilities.setCustomFont( context, holder.tvBody );
                    // holder.tvDate = ( TextView ) view.findViewById( R.id.date );
                    holder.ivPhoto = ( QuickContactBadge ) view.findViewById( R.id.imageContactPicture );
                    holder.btnDownload = ( Button ) view.findViewById( R.id.downloadButton );
                    holder.media = ( ImageView ) view.findViewById( R.id.media );
                    holder.ellipse = ( ImageView ) view.findViewById( R.id.ellipsis );

                    view.setTag( holder );

                }
                else
                {

                    holder = ( ViewHolder ) view.getTag();

                }

            }

        }

    }

我不确定我的getView()实现是否正确。下面一个是片段中的listview multichoise监听器

lvMessage.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE_MODAL );
        lvMessage.setMultiChoiceModeListener( new MultiChoiceModeListener()
        {
            private int nr = 0;
            private int longClickposition;
          @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position,
                                                  long id, boolean checked) {
                // Here you can do something when items are selected/de-selected,
                // such as update the title in the CAB
                MainActivity.pager.setScrollingEnabled( true );


              longClickposition=position;
              if (checked) {
                  nr++;
                  adapter.setNewSelection(position, checked);                    
              } else {
                  nr--;
                  adapter.removeSelection(position); 


              }

              mode.setTitle(nr + " selected");
        }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                // Respond to clicks on the actions in the CAB

                return false;
            }

            @Override
            public boolean onCreateActionMode(final ActionMode mode, Menu menu) {
                 nr = 0;
              //  inflater.inflate(R.menu.contextual_menu, menu);
                mode.getMenuInflater().inflate( R.menu.contextual_menu, menu );

                return true;

            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                // Here you can make any necessary updates to the activity when
                // the CAB is removed. By default, selected items are deselected/unchecked.
               // Log.e("onDestroyActionMode","onDestroyActionMode");
              //  MainActivity.pager.setScrollingEnabled( true );
                adapter.clearSelection();

            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

                return false;
            }
        });

    }

有没有遇到过类似的问题?

0 个答案:

没有答案