multichoicemodelistener选择多个项目进行单击

时间:2015-09-11 18:47:45

标签: listview android-cursoradapter multichoiceitems

我已经实现了MultichoicemodeListener并且它创建了一些奇怪的问题...我在列表中有12个项目... 8个可见,4个在屏幕下方..所以这4个是隐藏的。当我从8个可见项目中长按...代码也改变了隐藏视图中另一个的背景......休息功能正在工作.Plz帮助..我被困在最近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);
}

1 个答案:

答案 0 :(得分:0)

经过大量的谷歌搜索和尝试多种布局解决方案....我终于找到了答案....但首先让我们了解问题及其原因:

原因:多个列表项突出显示(选择正常工作/突出显示并选择不同的东西),因为滚动列表时回显ListView视图。滚动时重复使用突出显示的视图,以便突出显示下一个可见视图

解决方案:只需在style.xml中添加样式

即可
<style name="activated" parent="AppTheme">
    <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>

现在转到您正在使用的自定义布局,将其置于root中并进行设置。

style="@style/activated"

如果要覆盖默认选择颜色,也可以通过定义包含可激活模式的可绘制的新样式。

我唯一需要删除的是自定义边框以实现此功能。休息所有代码保持不变。

对Jonathan727来说,他的时间和精力都很大。真的很感激。