我已经实现了 MultichoicemodeListener 并且它产生了一些奇怪的问题......我在列表中有 12 个项目...... 8 个可见,4 个在屏幕下方.. 所以这 4 个是隐藏的。当我从 8 个可见项目中长按时......该代码还从隐藏视图中更改了另一个的背景......其余功能正在工作。请帮助..我从过去 2 天开始就被卡住了。
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();
}
请在下面找到光标适配器的代码
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);
}