Im developing a sms application where the UI setup is something like this.A Message fragment is used to show each message thread as a conversation list and a ResoruceCursorAdapter is used to bind conversation to the listview.Depending up on the message thread i will create fragments and im loading this fragment to a viewpager. So that i can swipe through each message thread as a fragment.Now in to the problem.I want to add contexual action bar to select multiple messages from the listview .Here the problem i face is its not highlighting the correct list row.When ever i select the items in the listview only the listview in the last fragment is getting selected
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();
}
}
}
}
Im not sure whether my getView() implementation is correct or not . And below one is the listview multichoise listener in the fragment
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;
}
});
}
Does any have experienced similar problem ?