Multiple Selection in Custom Listview with Cab

Multiple selection in custom ListView with CAB

Using ActionBarSherlock the MultiChoiceModeListener used in Luksprog´s answer is not yet available if you want to support API level < 11.

A workaround is to use the onItemClickListener.

List setup:

listView = (ListView) timeline.findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
listView.setAdapter(new ListAdapter(getActivity(), R.layout.cleaning_list_item, items));

Listener of ListFragment or ListActivity:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
SparseBooleanArray checked = listView.getCheckedItemPositions();
boolean hasCheckedElement = false;
for (int i = 0; i < checked.size() && !hasCheckedElement; i++) {
hasCheckedElement = checked.valueAt(i);
}

if (hasCheckedElement) {
if (mMode == null) {
mMode = ((SherlockFragmentActivity) getActivity()).startActionMode(new MyActionMode());
mMode.invalidate();
} else {
mMode.invalidate();
}
} else {
if (mMode != null) {
mMode.finish();
}
}
}

Where MyActionMode is an implementation of ActionMode.Callback:

private final class MyActionMode implements ActionMode.Callback { /* ... */ }

Android contextual action bar with custom ListView not allowing multiple selection

Multiple selection not exist

Selected items (rows) not colored to indicate they are selected

Presumably, you are not setting up the activated style for your list rows, which governs both of these.

How to change... color of the CAB

A custom theme should be able to do it. The rules should vary somewhat based on whether you are using the native action bar, older versions of appcompat-v7, or current editions of appcompat-v7. There are plenty of existing Stack Overflow material on this.

Show some text on the CAB

There is setTitle() and setSubtitle() on ActionMode that you can call.

See this sample app for a demonstration of using the activated style and titles/subtitles. In it, the ListView is in normal mode until the user long-presses on a row, in which case it switches to multiple-choice-modal operation.

Creating multiple selection list Android

Use checkedlistview it Help you

ListMain.Java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressLint("ShowToast")
public class ListMain extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
ListView mylist = (ListView) findViewById(R.id.companionsearch_listView1);
String[] list={"one","two","three"};
ArrayAdapter adapter = new ArrayAdapter<String>(ListMain.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setAdapter(adapter);

}
}

activity_main.xml

<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" >

<LinearLayout
android:id="@+id/bottombarbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:baselineAligned="false"
android:weightSum="3" >

<ListView
android:id="@+id/companionsearch_listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#ffffff" >
</ListView>
</LinearLayout>

</RelativeLayout>

Output:

Sample Image

delete multiple items in custom listview

This is how i created my custom listview with ease:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DeleteData extends Activity {

Cursor cursor;
ListView lv_delete_data;
static ArrayList<Integer> listOfItemsToDelete;
SQLiteDatabase database;
private Category[] categories;
ArrayList<Category> categoryList;
private ArrayAdapter<Category> listAdapter;
ImageView iv_delete;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_data);

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

manage_reload_list();

listOfItemsToDelete = new ArrayList<Integer>();

iv_delete = (ImageView) findViewById(R.id.imageViewDeleteDataDelete);
iv_delete.setClickable(true);
iv_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listOfItemsToDelete.isEmpty()) {

Toast.makeText(getBaseContext(), "No items selected.",
Toast.LENGTH_SHORT).show();
}

else {
showDialog(
"Warning",
"Are you sure you want to delete these categories ? This will erase all records attached with it.");
}
}
});

lv_delete_data = (ListView) findViewById(R.id.listViewDeleteData);
lv_delete_data.setAdapter(listAdapter);
lv_delete_data.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ImageView imageView = (ImageView) arg1
.findViewById(R.id.imageViewSingleRowDeleteData);
Category category = (Category) imageView.getTag();

if (category.getChecked() == false) {
imageView.setImageResource(R.drawable.set_check);
listOfItemsToDelete.add(category.getId());
category.setChecked(true);
} else {
imageView.setImageResource(R.drawable.set_basecircle);
listOfItemsToDelete.remove((Integer) category.getId());
category.setChecked(false);
}
}
});
}

private void showDialog(final String title, String message) {

AlertDialog.Builder adb = new Builder(DeleteData.this);
adb.setTitle(title);
adb.setMessage(message);
adb.setIcon(R.drawable.ic_launcher);
String btn = "Ok";
if (title.equals("Warning")) {
btn = "Yes";
adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}

adb.setPositiveButton(btn, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {

if (title.equals("Warning")) {

for (int i : listOfItemsToDelete) {

// -------first delete from category table-----

database.delete("category_fields", "cat_id=?",
new String[] { i + "" });

// --------now fetch rec_id where cat_id =
// i-----------------

cursor = database.query("records_data",
new String[] { "rec_id" }, "cat_id=?",
new String[] { i + "" }, null, null, null);
if (cursor.getCount() == 0)
cursor.close();
else {
ArrayList<Integer> al_tmp_rec_id = new ArrayList<Integer>();
while (cursor.moveToNext())
al_tmp_rec_id.add(cursor.getInt(0));
cursor.close();

for (int i1 : al_tmp_rec_id) {
database.delete("records_fields_data",
"rec_id=?", new String[] { i1 + "" });

database.delete("record_tag_relation",
"rec_id=?", new String[] { i1 + "" });
}

// ---------delete from records_data-------

database.delete("records_data", "cat_id=?",
new String[] { i + "" });

cursor.close();
}
}

showDialog("Success",
"Categories, with their records, deleted successfully.");
} else {
database.close();
listOfItemsToDelete.clear();
manage_reload_list();
lv_delete_data.setAdapter(listAdapter);
}
}
});

AlertDialog ad = adb.create();
ad.show();
}

protected void manage_reload_list() {

loadDatabase();

categoryList = new ArrayList<Category>();

// ------first fetch all categories name list-------

cursor = database.query("category", new String[] { "cat_id",
"cat_description" }, null, null, null, null, null);
if (cursor.getCount() == 0) {
Toast.makeText(getBaseContext(), "No categories found.",
Toast.LENGTH_SHORT).show();
cursor.close();
} else {

while (cursor.moveToNext()) {

categories = new Category[] { new Category(cursor.getInt(0),
cursor.getString(1)) };
categoryList.addAll(Arrays.asList(categories));
}
cursor.close();
}
listAdapter = new CategoryArrayAdapter(this, categoryList);
}

static class Category {

String cat_name = "";
int cat_id = 0;
Boolean checked = false;

Category(int cat_id, String name) {
this.cat_name = name;
this.cat_id = cat_id;
}

public int getId() {
return cat_id;
}

public String getCatName() {
return cat_name;
}

public Boolean getChecked() {
return checked;
}

public void setChecked(Boolean checked) {
this.checked = checked;
}

public boolean isChecked() {
return checked;
}

public void toggleChecked() {
checked = !checked;
}
}

static class CategoryViewHolder {

ImageView imageViewCheck;
TextView textViewCategoryName;

public CategoryViewHolder(ImageView iv_check, TextView tv_category_name) {
imageViewCheck = iv_check;
textViewCategoryName = tv_category_name;
}

public ImageView getImageViewCheck() {
return imageViewCheck;
}

public TextView getTextViewCategoryName() {
return textViewCategoryName;
}
}

static class CategoryArrayAdapter extends ArrayAdapter<Category> {

LayoutInflater inflater;

public CategoryArrayAdapter(Context context, List<Category> categoryList) {

super(context, R.layout.single_row_delete_data,
R.id.textViewSingleRowDeleteData, categoryList);
inflater = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Category category = (Category) this.getItem(position);

final ImageView imageViewCheck;
final TextView textViewCN;

if (convertView == null) {

convertView = inflater.inflate(R.layout.single_row_delete_data,
null);

imageViewCheck = (ImageView) convertView
.findViewById(R.id.imageViewSingleRowDeleteData);
textViewCN = (TextView) convertView
.findViewById(R.id.textViewSingleRowDeleteData);

convertView.setTag(new CategoryViewHolder(imageViewCheck,
textViewCN));
}

else {

CategoryViewHolder viewHolder = (CategoryViewHolder) convertView
.getTag();
imageViewCheck = viewHolder.getImageViewCheck();
textViewCN = viewHolder.getTextViewCategoryName();
}

imageViewCheck.setFocusable(false);
imageViewCheck.setFocusableInTouchMode(false);
imageViewCheck.setClickable(true);
imageViewCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageView iv = (ImageView) v;
Category category = (Category) iv.getTag();

if (category.getChecked() == false) {
imageViewCheck.setImageResource(R.drawable.set_check);
listOfItemsToDelete.add(category.getId());
category.setChecked(true);
} else {
imageViewCheck
.setImageResource(R.drawable.set_basecircle);
listOfItemsToDelete.remove((Integer) category.getId());
category.setChecked(false);
}
}
});
imageViewCheck.setTag(category);

if (category.getChecked() == true)
imageViewCheck.setImageResource(R.drawable.set_check);
else
imageViewCheck.setImageResource(R.drawable.set_basecircle);

textViewCN.setText(category.getCatName());

return convertView;
}
}

private void loadDatabase() {
database = openOrCreateDatabase("WalletAppDatabase.db",
SQLiteDatabase.OPEN_READWRITE, null);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

Anyone who have doubts in this can ask me...

Deleting multiple selected items in custom listview dosn't work with ImageButton

Finally, I found the answer :
we have to set

    android:descendantFocusability="blocksDescendants"

in root list item element and set

    android:focusableInTouchMode="true"

in the ImageButton view.



Related Topics



Leave a reply



Submit