Multiple Row Item Deleted from Listview Android


In this article we are learning the new android API concept that delete the multiple  row item of the list view.
Before we have knowledge about the delete the row item with the help of check box. But in new API-18 have provide the features to delete the multiple row item from the list-view without the check-box uses.

This feature is available inside the contextual action mode in higher android version 3.0. So here question is what is contextual action mode?
The contextual action mode is a system implementation of ActionMode that focuses user interaction toward performing contextual actions. When a user enables this mode by selecting an item, a contextual action bar appears at the top of the screen to present actions the user can perform on the currently selected item(s). While this mode is enabled, the user can select multiple items (if you allow it), deselect items, and continue to navigate within the activity (as much as you're willing to allow). The action mode is disabled and the contextual action bar disappears when the user deselects all items, presses the BACK button, or selects the Done action on the left side of the bar.

This event happen when the user performs a long-click on the view.  Contextual actions on groups of items in a ListView or GridView (allowing the user to select multiple items and perform an action on them all).
More details about this contextual menu. and MultiChoiceModeListener.

So lets start the to coding to see this features. 


main_activity.xml

?
1
2
3
4
5
6
7
8
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
     
    <listview android:id="@+id/listview" android:layout_height="fill_parent" android:layout_width="fill_parent">
     
</listview></relativelayout>

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.sunil.listviewmuntilerowdelete;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ListView;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements MultiChoiceModeListener{
  private String[] myfriendname=null;
  private String[] myfriendnickname=null;
  private int[] photo=null;
  ListView listView=null;
  Context contex=null;
  MyListAdapter adapter=null;
  private List<myfriendssdetails> list=new ArrayList<myfriendssdetails>();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  contex=this;
  listView = (ListView) findViewById(R.id.listview);
   
  myfriendname = new String[] { "Sunil Gupta", "Abhishek Tripathi","Sandeep Pal", "Amit Verma" };
        myfriendnickname = new String[] { "sunil android", "Abhi cool","Sandy duffer", "Budhiya jokar"};
        photo = new int[] { R.drawable.sunil, R.drawable.abhi, R.drawable.sandy, R.drawable.amit};
      
        for(int index=0; index< myfriendname.length; index++){
         MyFriendsSDetails details=new MyFriendsSDetails(myfriendname[index], myfriendnickname[index], photo[index]);
         list.add(details);
        }
         
        adapter=new MyListAdapter(contex, list);
        listView.setAdapter(adapter);
        listView.setMultiChoiceModeListener(this);
        listView.setChoiceMode(listView.CHOICE_MODE_MULTIPLE_MODAL);
  
 }
 @Override
 public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
  switch (arg1.getItemId()) {
  case R.id.delete:
   SparseBooleanArray selected = adapter.getSelectedIds();
   for (int i = (selected.size() - 1); i >= 0; i--) {
    if (selected.valueAt(i)) {
     MyFriendsSDetails selecteditem = adapter.getItem(selected.keyAt(i));
     adapter.remove(selecteditem);
    }
   }
   // Close CAB
   arg0.finish();
   return true;
   default:
    return false;
  }
   }
 @Override
 public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
  arg0.getMenuInflater().inflate(R.menu.main, arg1);
  return true;
   
 }
 @Override
 public void onDestroyActionMode(ActionMode arg0) {
  adapter.removeSelection();
 }
 @Override
 public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
  return false;
 }
 @Override
 public void onItemCheckedStateChanged(ActionMode arg0, int arg1, long arg2, boolean arg3) {
   
  final int checkedCount = listView.getCheckedItemCount();
  arg0.setTitle(checkedCount + " Selected");
  adapter.toggleSelection(arg1);
 }
}
</myfriendssdetails></myfriendssdetails>

MyFriendsSDetails.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.sunil.listviewmuntilerowdelete;
public class MyFriendsSDetails {
  
  private String myfriendname=null;
  private String myfriendnickname=null;
  private int photo=0;
   
  public MyFriendsSDetails(String friendname, String friendnickname, int myphoto){
   this.myfriendname=friendname;
   this.myfriendnickname=friendnickname;
   this.photo=myphoto;
  }
 public String getMyfriendname() {
  return myfriendname;
 }
 public void setMyfriendname(String myfriendname) {
  this.myfriendname = myfriendname;
 }
 public String getMyfriendnickname() {
  return myfriendnickname;
 }
 public void setMyfriendnickname(String myfriendnickname) {
  this.myfriendnickname = myfriendnickname;
 }
 public int getPhoto() {
  return photo;
 }
 public void setPhoto(int photo) {
  this.photo = photo;
 }
   
}

list_item.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
<linearlayout android:background="?android:attr/activatedBackgroundIndicator" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingbottom="5dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <imageview android:adjustviewbounds="true" android:id="@+id/icon" android:layout_height="50dp" android:layout_width="50dp" android:scaletype="centerCrop">
    <linearlayout android:gravity="center_vertical|left" android:layout_height="match_parent" android:layout_width="wrap_content" android:orientation="vertical" android:padding="5dp">
        <textview android:ellipsize="end" android:id="@+id/title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:singleline="true" android:textsize="15sp">
        <textview android:ellipsize="end" android:id="@+id/subtitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:singleline="true" android:textsize="10sp">
    </textview></textview></linearlayout>
</imageview></linearlayout>

MyListAdapter.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.sunil.listviewmuntilerowdelete;
import java.util.List;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<myfriendssdetails>{
  
 Context context;
 LayoutInflater inflater;
 List<myfriendssdetails> list;
 private SparseBooleanArray mSelectedItemsIds;
  
 public MyListAdapter(Context context, List<myfriendssdetails> list) {
  super(context, 0, list);
  mSelectedItemsIds = new SparseBooleanArray();
  this.context = context;
  this.list = list;
  inflater = LayoutInflater.from(context);
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
   
  final ViewHolder holder;
  if (convertView == null) {
   holder = new ViewHolder();
   convertView = inflater.inflate(R.layout.list_item, null);
   holder.name = (TextView) convertView.findViewById(R.id.title);
   holder.nickname = (TextView) convertView.findViewById(R.id.subtitle);
   holder.photo = (ImageView) convertView.findViewById(R.id.icon);
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  holder.name.setText(list.get(position).getMyfriendname());
  holder.nickname.setText(list.get(position).getMyfriendnickname());
  holder.photo.setImageResource(list.get(position).getPhoto());
  return convertView;
 }
  
 public void toggleSelection(int position) {
  selectView(position, !mSelectedItemsIds.get(position));
 }
 public void selectView(int position, boolean value) {
  if (value)
   mSelectedItemsIds.put(position, value);
  else
   mSelectedItemsIds.delete(position);
  notifyDataSetChanged();
 }
  
 public void removeSelection() {
  mSelectedItemsIds = new SparseBooleanArray();
  notifyDataSetChanged();
 }
  
 public SparseBooleanArray getSelectedIds() {
  return mSelectedItemsIds;
 }
 private class ViewHolder {
  TextView name;
  TextView nickname;
  ImageView photo;
 }
}
</myfriendssdetails></myfriendssdetails></myfriendssdetails>
Enjoy!...


Comments

Popular posts from this blog