I try to do this with setMultiChoiceModeListener
but is not working. When i select the multiple item of listview its not highlight that item. I do many things but its not working.See my code is given bellow.
Listview in xml file.
<ListView
android:id="@+id/lv_ChatMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_msg"
android:background="?android:attr/activatedBackgroundIndicator"
android:divider="@color/main_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:smoothScrollbar="true"
android:stackFromBottom="true" />
This is my MessageAdapter.java
for message adapter.
private SparseBooleanArray SelectedItemsId;
public ArrayList<MessageList> arr_Message;
public void toggleSelection(int position) {
// TODO Auto-generated method stub
selectView(position, !SelectedItemsId.get(position));
}
private void selectView(int position, boolean value) {
// TODO Auto-generated method stub
Log.e("remove", "position-- " + position + "value-- " + value);
if (value) {
SelectedItemsId.put(position, value);
// rl_MsgItem.setBackgroundResource(R.color.outofstoke);
} else {
SelectedItemsId.delete(position);
// rl_MsgItem.setBackgroundResource(android.R.color.transparent);
notifyDataSetChanged();
}
}
public int getSelectedCount() {
Log.e("getSelectedCount",
"getSelectedCount---- " + SelectedItemsId.size());
return SelectedItemsId.size();
}
public SparseBooleanArray getSelectedIds() {
Log.e("SparseBooleanArray", "SparseBooleanArray");
return SelectedItemsId;
}
public void remove(MessageList messageList) {
// TODO Auto-generated method stub
Log.e("remove", "remove");
arr_Message.remove(messageList);
removeSelection();
}
public void removeSelection() {
Log.e("removeSelection", "removeSelection");
SelectedItemsId = new SparseBooleanArray();
notifyDataSetChanged();
}
And my code in MessageActivit.java
is bellow.
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
mode.getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.delete:
// Calls getSelectedIds method from ListViewAdapter Class
SparseBooleanArray selected = adapter.getSelectedIds();
// Captures all selected ids with a loop
for (int i = (selected.size() - 1); i >= 0; i--) {
if (selected.valueAt(i)) {
MessageList selecteditem = (MessageList) adapter
.getItem(selected.keyAt(i));
// Remove selected items following the ids
arr_Message.remove(selecteditem);
}
}
mode.finish();
adapter = new MessageAdapter(getApplicationContext(), arr_Message);
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// TODO Auto-generated method stub
if (checked == true) {
Log.e("Position", "" + position);
} else {
Log.e("Position", "" + position);
}
final int checkedCount = lv_ChatMsg.getCheckedItemCount();
mode.setTitle(checkedCount + " Selected Message");
adapter.toggleSelection(position);
}
Do like this in your item xml file .
<Linerlayout
...
android:background="?android:attr/activatedBackgroundIndicator"
... >
other your item code.
</Linerlayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.