I have implemented MultichoicemodeListener and its creating some weird issues... I have 12 items on list... 8 are visible and 4 are below the screen .. so those 4 are hidden .when i long click from 8 visible items...the code also changes the background of another one from hidden views......rest functions are working.Plz help.. i am stuck from last 2 days.
MyCusrsorAdaptor customAdaptor;
private Cursor mCursor;
private ListView lv01;
View view;
int itemsSelectedCount=0;
SelectedItems = new SparseBooleanArray();
lv01.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv01.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
itemsSelectedCount = toggleSelection(position, checked);
mode.setTitle(itemsSelectedCount + " Messages Selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.menu_multichoice, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_msg: {
DeleteSelectedRows();
}
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
SelectedItems.clear();
SelectedItems = new SparseBooleanArray();
lv01.clearChoices();
recreate();
}
});
}
private void DeleteSelectedRows()
{
for(int k =0; k< SelectedItems.size();k++)
{
Cursor curitem = (Cursor) lv01.getItemAtPosition(SelectedItems.keyAt(k));
String rowID = curitem.getString(curitem.getColumnIndex(dbHelper.COL_ROWID));
String whereclause = dbHelper.COL_ROWID+"=?";
db.delete(dbHelper.TABLE_NAME3,whereclause,new String[]{rowID});
Log.d(TAG, "Delteted ROWID is --> "+rowID);
}
UpdateAlarms();
recreate();
}
private int toggleSelection(int position, boolean checked)
{
RelativeLayout relativeLayout = (RelativeLayout)lv01.getChildAt(position);
if(checked==true)
{ SelectedItems.put(position,checked);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.border_multichoice));
}
else{
SelectedItems.delete(position);
relativeLayout.setBackground(getResources().getDrawable(R.drawable.borderedittask));
}
return SelectedItems.size();
}
Please find below the code for Cursor Adaptor
public class MyCusrsorAdaptor extends CursorAdapter
{
private LayoutInflater curinf;
private MyDBHelper dbHelper;
private static final String TAG="OK OK OK OK OK";
public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date date;
public MyCusrsorAdaptor(Context context, Cursor c, int flags) {
super(context, c, flags);
curinf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
TextView title = (TextView) view.findViewById(R.id.title);
String titleName = cursor.getString(cursor.getColumnIndex(dbHelper.COL_USERNAME));
title.setFocusable(false);
title.setFocusableInTouchMode(false);
ImageView imv = (ImageView) view.findViewById(R.id.list_image);
imv.setFocusable(false);
imv.setFocusableInTouchMode(false);
TextView artist1 = (TextView) view.findViewById(R.id.artist1);
TextView artist2 = (TextView) view.findViewById(R.id.artist2);
artist1.setFocusable(false);
artist1.setFocusableInTouchMode(false);
artist2.setFocusable(false);
artist2.setFocusableInTouchMode(false);
String userid = cursor.getString(cursor.getColumnIndex(dbHelper.COL_1));
String userRel = cursor.getString(cursor.getColumnIndex(dbHelper.COL_2));
String dur = cursor.getString(cursor.getColumnIndex(dbHelper.COL_3));
String mstyp = cursor.getString(cursor.getColumnIndex(dbHelper.COL_4));
String msg = cursor.getString(cursor.getColumnIndex(dbHelper.COL_5));
try {
date = dateFormat.parse(cursor.getString(cursor.getColumnIndex(dbHelper.COL_6)));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
artist1.setText("Lets Rock on: "+ PrintTypeDateFormatter(date));
artist2.setText(msg);
title.setText(titleName+" ("+userid+" )");
}
private String PrintTypeDateFormatter(Date date)
{
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE dd MMM yyyy hh:mm a");
String toPrint = dateFormat2.format(date).toString();
android.util.Log.d(TAG, toPrint);
return toPrint;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return curinf.inflate(R.layout.custom_layout_messages_type,parent,false);
}
After lot of Googling and trying multiple Layout solutions....I finally found the answer....But first lets understand the issue and its cause:
Cause: Multiple list Items were getting highlighted(Selection working fine / Highlighted and selected are different things) because of Recycling Views of ListView while scrolling the list.The view which was highlighted was reused while scrolling so next visible view was also highlighted.
Solution: Just add a style in style.xml
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
Now go to custom layout which you are using put this in root and you are set.
style="@style/activated"
If you want to override the default selection color then also can by defining new style which includes drawable for activated mode.
the only thing i had to remove was custom Border for this to work. rest all code remains same.
Thx a ton to Jonathan727 for his time and effort. Really appreciate.