how to get position in on context item selected in android? - android

setListAdapter(new ArrayAdapter<String>(this,R.layout.custom_list_item, r));
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
fname=r.get(position);
the above code I got position and name also from array adapter
Like, I need get these all are the values in context menu how can I get it
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Playlist Option");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
// here I select the particular list value, that the value position I need that is only I delete that position from server. what are the data i fetch here? and its possible to get position from array list?
}

You can use AdapterContextMenuInfo
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
String itemName = r.get[(int) info.id]; // This item you will delete
// from list
return true;
}
}

public class DataDemo extends Activity {
public ArrayList<HashMap<String, String>> ContactList = new ArrayList<HashMap<String, String>>();
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_PHNO = "phNo";
private static int itemIndex;
private ListView lst;
private List<Contact> contacts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database_demo);
lst = (ListView) findViewById(R.id.lstContact);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lstContact) {
menu.setHeaderIcon(R.drawable.message);
menu.setHeaderTitle("Contacts");
menu.add(0, 1, Menu.NONE, "Edit Contact");
menu.add(0, 2, Menu.NONE, "Delete Contact");
menu.add(0, 3, Menu.NONE, "Cancel");
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterView.AdapterContextMenuInfo menuInfo;//you can select on context item selected
menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
itemIndex = menuInfo.position;//you will get position of selected item
final Contact con = contacts.get(itemIndex);
switch (item.getItemId()) {
case 1:
Intent editIntent = new Intent(DataDemo.this, UpdateContact.class);
editIntent.putExtra(TAG_ID, con.getID());
editIntent.putExtra(TAG_NAME, con.getName());
editIntent.putExtra(TAG_PHNO, con.getPhNo());
startActivity(editIntent);
break;
case 2:
new AlertDialog.Builder(this)
.setIcon(R.drawable.document_delete)
.setTitle("Confirm Delete")
.setMessage("Are you sure?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
DatabaseHandler db = new DatabaseHandler(
DataDemo.this);
db.DeleteContact(con);
Toast.makeText(DataDemo.this, con.getName() + " Deleted",
Toast.LENGTH_SHORT).show();
fillList();
}
}).setNegativeButton("No", null).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem addNew = menu.add(0, 1, 0, "Create Contact");
addNew.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent iCreate = new Intent(DataDemo.this, CreateContact.class);
startActivity(iCreate);
return true;
}
});
return true;
}
protected void fillList() {
DatabaseHandler db = new DatabaseHandler(this);
ListAdapter adap;
ContactList.clear();
contacts = db.ReadAllContact();
for (Contact cn : contacts) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put(TAG_NAME, cn.getName());
hmap.put(TAG_PHNO, cn.getPhNo());
ContactList.add(hmap);
adap = new SimpleAdapter(this, ContactList,
R.layout.list_item_view,
new String[] { TAG_NAME, TAG_PHNO }, new int[] {
R.id.lstName, R.id.lstPhNo });
lst.setAdapter(adap);
registerForContextMenu(lst);
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
fillList();
}
}

Related

How to get ListView's textview for intent in ContextMenu?

I have a ListView. This ListView has an array and the array has 5 numbers. First of all, in the first activity the user selects 5 numbers and theses 5 number go in the ListView in the second activity. I want to start an Intent with theses 5 numbers when the user click on any number on the Context menu. The intent will start that position. But why cant I get the selected number?
public class AnaMenu extends AppCompatActivity {
public static final int MENU_Ara = Menu.FIRST+1;
int sayı = 50;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ana_menu);
ListView liste = (ListView) findViewById(R.id.oyun);
OyunTextView oyunTextView = new OyunTextView();
liste.setAdapter(oyunTextView);
registerForContextMenu(liste);
}
class OyunTextView extends BaseAdapter {
#Override
public int getCount() {
Intent i = getIntent();
final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");
return secilmis.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.oyuntextview , null);
TextView namesbox = (TextView)convertView.findViewById(R.id.isim);
TextView sayılar = (TextView)convertView.findViewById(R.id.sayı);
Intent i = getIntent();
final ArrayList<String> secilmis = i.getStringArrayListExtra("listem");
namesbox.setText(secilmis.get(position));
sayılar.setText(String.valueOf(sayı));
if (sayı == 50) {
sayılar.setTextColor(getResources().getColor(R.color.elli));}
return convertView;
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, MENU_Ara, Menu.NONE, "Ara");
super.onCreateContextMenu(menu, v, menuInfo);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Intent i = getIntent();
secilmis = i.getStringArrayListExtra("listem");
switch (item.getItemId()) {
case MENU_Ara:
Intent ara = new Intent(Intent.ACTION_DIAL);
ara.setData(Uri.parse());
startActivity(ara);
return true;
}
return super.onContextItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.mains,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_browse :ayar();return true;
case R.id.backbut: geri();return true;
}
return super.onOptionsItemSelected(item);
}
private void ayar() {
Intent i = new Intent(AnaMenu.this, Ayarlar.class);
startActivity(i);
}
private void geri() {
Intent ii = new Intent(AnaMenu.this, MainActivity.class);
startActivity(ii);
AnaMenu.this.finish();
}
}
}
You can use arraylistname.get(position) in the onItemClickListener method to get the position of the selected item.

multiple listview in one activity with contextmenu for each of listview

I have multiple ListView in one activity with ContextMenu for each one of ListView.
Actually it should work fine with all the 7 ListViews,but it work for only Seventh List
this is my code.
ArrayAdapter<Course> adapter;
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
db.removeCourse(adapter.getItem(info.position);
adapter.remove(adapter.getItem(info.position));
return true;
default:
return super.onContextItemSelected(item);
}
}
please help
Adapter instance is set to last iteration ListView object. This is quick code change. Try this:
private ArrayAdapter<Course> selectedListViewAdapter;
...
for(int i=0;i<7;i++){
courses = db.findFiltered(String.format("day == %d ", i), "startTime ASC");
ListView lv1 = (ListView) findViewById (arrayList[i]);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<Course> adapter = new CourseListAdapter(this, courses);
lv1.setAdapter(adapter);
}
#Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
try {
ListView selectedListView = (ListView)v;
selectedListViewAdapter = (ArrayAdapter<Course>)selectedListView.getAdapter();
} catch(ClassCastException e) {
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.editItem:
//do something
return true;
case R.id.showItem:
//do something
return true;
case R.id.deleteItem:
if(null != selectedListViewAdapter) {
db.removeCourse(selectedListViewAdapter.getItem(info.position);
selectedListViewAdapter.remove(selectedListViewAdapter.getItem(info.position));
}
return true;
default:
return super.onContextItemSelected(item);
}
}

How to get position of clicked listview in single clicked contextmenu

I have a defined listview in xml and add the data by modifying it through adapter..
I want to display context menu by single click the list, which I already done..
But how to get the clicked list position ?
Here is the code, this is how I call the context menu :
list = (ListView)findViewById(R.id.grant_list);
// get all data in database
List<GrantListData> dataList = con.getAllList();
try {
list.removeAllViews();
} catch (Exception e) {}
listAll.clear();
arrName.clear();
arrPhone.clear();
for (int i = 0; i < dataList.size(); ++i) {
listAll.add(dataList.get(i));
arrName.add(dataList.get(i).getName().toString());
arrPhone.add(dataList.get(i).getPhone().toString());
}
adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, arrName);
list.setAdapter(adapter);
this.registerForContextMenu(list);
list.setOnLongClickListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String item = (String)parent.getItemAtPosition(pos);
activity.registerForContextMenu(view);
activity.openContextMenu(view);
}
});
and this is onCreateContextMenu method :
int num=-1;
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
//String clicked = adapter.getItem(Info.position); --> this is when I get confused
//num = Info.position;
//menu.setHeaderTitle("Options for " + clicked);
menu.add(1, 1, 1, "Details");
//menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
//menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
menu.add(1, 2, 2, "Delete");
}
[ADDITION]
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
Thanks in advance :)
[SOLVED]
Well, it took a while for me to realize simple thing. What I mean is this
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
currList = (String)parent.getItemAtPosition(pos);
currIndex = pos;
registerForContextMenu(view);
openContextMenu(view);
}
});
and this
int num=-1;
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle("Options for "+ currList);
menu.add(1, 1, 1, "Details");
menu.addSubMenu(1, 1, 1, arrName.get(currIndex));
menu.addSubMenu(1, 1, 2, arrPhone.get(currIndex));
menu.add(1, 2, 2, "Delete");
}
Thanks for the answers anw :)
You set a tag with position for your view and then set the groupid for your menu item by this position.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String item = (String)parent.getItemAtPosition(pos);
view.setTag(position);
activity.registerForContextMenu(view);
activity.openContextMenu(view);
}
});
put the position in the groupid of the menu item which you can retrieve by getGroupId
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo Info = (AdapterContextMenuInfo) menuInfo;
//String clicked = adapter.getItem(Info.position); --> this is when I get confused
int position = Integer.parseInt(v.getTag());
//num = Info.position;
//menu.setHeaderTitle("Options for " + clicked);
menu.add(position, 1, 1, "Details");
//menu.addSubMenu(1, 1, 1, arrName.get(Info.position));
//menu.addSubMenu(1, 1, 2, arrPhone.get(Info.position));
menu.add(position, 2, 2, "Delete");
}
try this one:
((ListView) findViewById(R.id.lisview)).setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
long arg3) {
// TODO Auto-generated method stub
/*"Toast the int which is arg2 to show the position of listview that has been clicked"*/
}
});
Using Context menu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Do you want to add?");
MenuItem chix = menu.add("Add Chicken");
MenuItem pasta = menu.add("Add Pasta");
MenuItem drinks = menu.add("Add Drinks");
MenuItem con = menu.add("Continue");
chix.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
pasta.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
drinks.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
con.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
//your codes here//
return true;
}
});
}

Android: Issue while using ContextMenu on Dialog

I have created a context menu. The context menu appears, when I do a longclick on the listitems. So far so good...
But when i click on a contextitem, nothing happens. Does anyone know this issue?
What's the problem here?
Button for opening the dialog with listview:
Button cmd_fav = (Button) findViewById(R.id.cmd_main_fav);
cmd_fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<String> valueList = new ArrayList<String>();
db = SQLiteDatabase.openDatabase("/data/data/spicysoftware.abugrundwissen/databases/questions", null,
SQLiteDatabase.OPEN_READWRITE);
Cursor c_ = db.rawQuery("SELECT question, _id, answer FROM tbl_questions"+
" where favourite = 1", null);
if (c_ != null ) {
if (c_.moveToFirst()) {
do {
String str_question = c_.getString(c_.getColumnIndex("question"));
valueList.add(str_question);
} while (c_.moveToNext());
}
// custom dialog
dialog = new Dialog(MainSite.this);
dialog.setContentView(R.layout.dialog_list);
dialog.setTitle("Favoriten:");
adapter = new ArrayAdapter<String>(MainSite.this, android.R.layout.simple_list_item_1, valueList);
final ListView lv = (ListView)dialog.findViewById(R.id.list_search);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, CONTEXT_MENU_DELETE_ITEM, Menu.NONE, "Favorit entfernen");
menu.add(Menu.NONE, CONTEXT_MENU_FINISH_ITEM, Menu.NONE, "Frage abschliessen!");
}
});
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
String item = (String) lv.getItemAtPosition(position).toString();
Cursor c_2 = db.rawQuery("SELECT answer FROM tbl_questions"+
" where question = '"+item+"'", null);
if (c_2 != null ) {
if (c_2.moveToFirst()) {
answer = c_2.getString(c_2.getColumnIndex("answer"));
}
}
// custom dialog
final Dialog dialog = new Dialog(MainSite.this);
dialog.setContentView(R.layout.dialog_answer);
dialog.setTitle("Antwort:");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.txt_answer);
//text.setText(answer);
text.setText(Html.fromHtml(answer), TextView.BufferType.SPANNABLE);
Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});
OnContextItemSelected:
#Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("tst", "lol");
switch (item.getItemId()) {
case CONTEXT_MENU_DELETE_ITEM:
Log.v("DELETED", "TRUE");
return true;
case CONTEXT_MENU_FINISH_ITEM:
Log.v("FINISHED", "TRUE");
return true;
}
Log.v("FINISHED", "LOL");
return false;
}
Best Regards
MSeiz5
I found a solution here Android: ContextMenu and ItemSelected in Context Menu
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//MenuInflater inflater = getMenuInflater();
//inflater.inflate(R.menu.context_menu, menu);
MenuItem delete = menu.add("delete");
MenuItem add = menu.add("add");
add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Log.d("ContextCheck","EDIT!");
Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
return true;
}
});
}
Is Working!
If MenuInflater used, more generic code:
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.ruleitem_menu, menu);
if (menuInfo instanceof AdapterView.AdapterContextMenuInfo){
AdapterView.AdapterContextMenuInfo adptrCmi = (AdapterContextMenuInfo) menuInfo;
String lsItem = currentRuleListView.getItemAtPosition(adptrCmi.position).toString();
menu.setHeaderTitle( lsItem);
}
//if Activity.onContextItemSelected not triggered, try the following lines
for (int i=0; i< menu.size();i++){
menu.getItem(i).setOnMenuItemClickListener(new OnMenuItemClickListener(){
#Override
public boolean onMenuItemClick(MenuItem item) {
return onContextItemSelected(item);
}
});
}

android delete file using context menu

I've got a Listview showing files currently on the SDcard. When you long press the file, a context menu pops up.
My question is: how do I pass in the selected item to the Context Menu in order to delete the file from the list, and is it possible to also delete it from the SDcard using this? My Code is as follows:
public class PlayListActivity extends ListActivity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle", "songDate" }, new int[] {
R.id.songTitle, R.id.songDate });
setListAdapter(adapter);
// setup ListView item
ListView lv = getListView();
registerForContextMenu(lv);
notifyDataSetChanged();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int songIndex = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
Bandboxstage.class);
// Sending songIndex to PlayerActivity
in.putExtra("songIndex", songIndex);
setResult(100, in);
// Closing PlayListView
finish();
}
});
}
private void notifyDataSetChanged() {
// TODO Auto-generated method stub
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
Toast.makeText(this, "Delete Called.", Toast.LENGTH_SHORT).show();
deleteFile(info.id);
return true;
case R.id.share:
Toast.makeText(this, "Share Called.", Toast.LENGTH_SHORT).show();
default:
return super.onContextItemSelected(item);
}
}
private void deleteFile(long id) {
// TODO Auto-generated method stub
}
}
Well your answer is in your implementation itself. If you notice, in your onContextItemSelected()
, the following statement brings in the info of the item you have selected in your main listview.
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
You can use info.position to find out the position of your item in the list and then get the object from your ArrayList using songsList.get(info.position).
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
Toast.makeText(this, "Delete Called.", Toast.LENGTH_SHORT).show();
//Make sure songsList is a global variable so that it can be accessed here.
HashMap<String, String> song = songsList.get(info.position);
//Call your delete function to delete the song.
return true;
case R.id.share:
Toast.makeText(this, "Share Called.", Toast.LENGTH_SHORT).show();
default:
return super.onContextItemSelected(item);
}
}
Refer this LINK it passes varialble on long click.
below is the deletefile function
file.delete() will delete the file.

Categories

Resources