Skip to content

Instantly share code, notes, and snippets.

@omerucel
Created July 11, 2015 14:10
Show Gist options
  • Save omerucel/c4eb9cdc1f38128474b7 to your computer and use it in GitHub Desktop.
Save omerucel/c4eb9cdc1f38128474b7 to your computer and use it in GitHub Desktop.
ListView multiple remove
package com.omerucel.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.orm.query.Select;
import java.util.List;
public class PlansActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
protected ListView listView;
protected PlanListAdapter plansAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plans);
listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setOnItemClickListener(this);
listView.setMultiChoiceModeListener(new ListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
final int checkedCount = listView.getCheckedItemCount();
switch (checkedCount) {
case 0:
mode.setSubtitle(null);
break;
case 1:
mode.setSubtitle(getResources().getString(R.string.one_item_selected));
break;
default:
mode.setSubtitle(checkedCount + " " + getResources().getString(R.string.items_selected));
break;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_select_menu, menu);
mode.setTitle(getResources().getString(R.string.select_plans));
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_remove_items:
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
for (int i = 0; i < plansAdapter.getCount(); i++) {
if (checkedItems.get(i, false)) {
Plan plan = (Plan) plansAdapter.getItem(i);
plan.delete();
}
}
refreshItems();
mode.finish();
break;
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
@Override
protected void onResume() {
super.onResume();
plansAdapter = new PlanListAdapter(this, R.layout.plan_item);
refreshItems();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_plans, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_new_plan:
Intent intent = new Intent(this, SavePlanActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, LocationsActivity.class);
intent.putExtra("planId", id);
startActivity(intent);
}
public void refreshItems() {
List<Plan> plans = Select.from(Plan.class).orderBy("startDate").orderBy("name").list();
plansAdapter.setList(plans);
listView.setAdapter(plansAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".PlansActivity"
android:background="@android:color/white">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_remove_items"
android:icon="@drawable/ic_remove_circle_white_24dp"
android:title="@string/remove_selected_items"
app:showAsAction="always" />
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment