Payday Loan Payday Loan

Archive for April 23rd, 2011

How to use ContextMenu on a listView

Hi,
I will show you how you can create a contextmenu for your listview.
What is a contextMenu? A contextMenu is something like “right click”, only that on smartphones you need to
longclick to trigger a ContextMenu. So, this will trigger additional options to a listview item.
Let’s say this is an ListActivity:

 
public class Main extends ListActivity{
@Override
public void OnCreate(Bundle savedInstanceState){
super.OnCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mylist=getListView();
//below we declare our contextMenu, and we add 2 options that we can click on LongClick
 
myList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
 
 
		@Override
		public void onCreateContextMenu(ContextMenu arg0, View arg1,
				ContextMenuInfo arg2) { 
 
			// TODO Auto-generated method stub
			arg0.setHeaderTitle(" Menu: ");//contextmenu
			arg0.add(0, 0,0, "Edit!");
		       	arg0.add(0, 1,0, "Delete!");
}
		});
}
//after this, we override the follwing method
 @Override
	 public boolean onContextItemSelected(MenuItem aItem) {
	 AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
	 /* Switch on the ID of the item, to get what the user selected. */
 
 
	 switch (aItem.getItemId()) {
	 case 0:
//do something	
 return true; /* true means: “we handled the event”. */
	 case 1:
//do somethingelse
return true;
}
	 return false;
	 }
 
}//close the class...
||||| 0 I Like It! |||||

Search

Popular

Sponsors