Hi Guys!!
First of all Happy Diwali to all my friends and fans.
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
This event happen when the user performs a long-click on the view. Contextual actions on groups of items in a
More details about this contextual menu. and MultiChoiceModeListener.
So lets start the to coding to see this features.
You can download the full source code ListViewMultipleRowDelete and here
Cheers Guys!!
First of all Happy Diwali to all my friends and fans.
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
MainActivity.java
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 Listlist=new ArrayList ();
@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.java
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
MyListAdapter.java
package com.sunil.listviewmuntilerowdelete;Screen shot:
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{
Context context;
LayoutInflater inflater;
Listlist;
private SparseBooleanArray mSelectedItemsIds;
public MyListAdapter(Context context, Listlist) {
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;
}
}
You can download the full source code ListViewMultipleRowDelete and here
Cheers Guys!!
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :