I'm working with a ListFragment and doing a onListItemClick. Everything works fine, but now I want to use a long Item Click (e.g setOnItemLongClickListener(new OnItemLongClickListener() for an Activity). How can I use this in my fragment?
Thanks!
Yes, the solution posted by tsync works for me. I too had ran into same problem and considered that this is not possible. I tried the above suggestion as follows:
public class ProjectsFragment extends ListFragment {
#Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();
return true;
}
});
and it worked!
Depending on what you want to realize you can use the given methods for context menues:
First register the View class which gets long pressed (inside your Fragment class):
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
registerForContextMenu(this.getListView());
}
Than implement these two methods, to create a context menu and do what ever you want when a menu item is clicked:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = this.getActivity().getMenuInflater();
inflater.inflate(R.menu.my_context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.add: // <-- your custom menu item id here
// do something here
return true;
default:
return super.onContextItemSelected(item);
}
}
This works for me
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
//Get your item here with the position
return true;
}
});
Related
I want when long pressing an item of my list to give an option delete and delete the item if it pressed.
//onCreate()
alreadyAddedFood = (ListView) findViewById(R.id.alreadyAddedList);
registerForContextMenu(alreadyAddedFood);
//END of onCreate()
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_general, menu);
menu.setHeaderTitle("Select The Action");
}
#Override
public boolean onContextItemSelected(MenuItem item){
if(item.getItemId()==R.id.delete){
//How to delete?
Toast.makeText(getApplicationContext(),"delete"+item,Toast.LENGTH_LONG).show();
}else{
return false;
}
return true;
}
UPDATE
I also have this class which i implement the onlongClickListener and it works fine but without giving the user the option to press delete like the photo below
public void alreadyAdded(String searchedMessage) {
itemsAdded.add(searchedMessage);
final ArrayAdapter<String>addedAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,itemsAdded);
alreadyAddedFood.setAdapter(addedAdapter);
alreadyAddedFood.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
// #Override
// public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// itemsAdded.remove(position);
// addedAdapter.notifyDataSetChanged();
// Toast.makeText(AddFood.this, "Item Deleted", Toast.LENGTH_LONG).show();
// return true;
// }
// });
}
Add this in your onContextItemSelected :
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); // init the info to get the position from
items.remove(info.position); // remove the item from the list
adapter.notifyDataSetChanged(); //updating the adapter
You can do Something like this:
private ListView ls;
ls.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int arg2, long arg3) {
ls.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
return false;
}
});
You can pop up a dialog and ask the user for his confirmation.
However, it is less aesthetic. I would recommend one of these:
After the long click, show a delete option (using the delete icon rather than text) in the app-bar.
Do not support long click, instead use an swipe-able rows with a delete option (again, with an icon) in the right/left of each row. (I will recommend this library.
(gif was taken from here)
I have the following code, which runs when I click on my list, but I need it to only pop up a menu when I long click, the short click does other things.
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
CreateWO.performClick();
return true;
case R.id.punchin:
final Country country = (Country) ListView1.getItemAtPosition(getwoindex());
punchin.performClick();
Toast.makeText(getApplicationContext(), "Work Order="+country.code, Toast.LENGTH_SHORT).show();
return true;
case R.id.punchout:
punchout.performClick();
return true;
default:
return super.onContextItemSelected(item);
}
}
For normal click on listView item use: listView.onItemClickListener and for long click use: listView.setOnItemLongClickListener as follows:
ListView lv =(ListView)findViewById(R.id.my_list);
----
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do something on long click
//like show contextMenu
return false;
}
});
EDIT:
For ContextMenu:
You can either call openContextMenu(view) in your onItemLongClick (as above) OR you can use registerForContextMenu(view), for example:
lv.setAdapter(adapter);
registerForContextMenu(lv);
By default contextMenu will open when a user long clicks your view. In this case you do not need to call onLongClick manually on the listView.
I have used the following code to set context menu for my listview. Now I want to create the onContextItemClickListener for that context menu. Apparently there no such method like listview.setOnContextItemClickListener. Kindly Help me out here.
ListView listView = new ListView(getApplicationContext());
#SuppressWarnings("unchecked")
ArrayAdapter listViewArrayAdapter = new ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, locations);
listView.setAdapter(listViewArrayAdapter);
listView.setFocusableInTouchMode(true);
listView.setOnFocusChangeListener(
new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean arg1) {
Log.i("SampleApp", "onFocusChanged() - view=" + arg0);
}
});
listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView adapterView, View view,
int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Log.i("SampleApp", "Click on position"+selectedPosition);
}
});
listView.setOnCreateContextMenuListener(
new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo;
menu.add(0, 0, 0, "Delete");
}
});
You should add:
// we register for the contextmneu
registerForContextMenu(lv);
to register for context menu (i.e lv is the instance of ListView) and then:
// This method is called when user selects an Item in the Context menu
#Override
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
// do your logic here
return true;
}
Hope this is what you are looking for.
PS: If you are interested i wrote a post about it, give a look here
I have a CustomListAdapter. I have overloaded my OnItemClickListener and added a registerForContextMenu line for the position that i have a context menu shown.
When i select the Item that should show the MenuItem, the menu is shown. When i press the back button on the phone the menu disappears. However now what happens that the same Item in my listview does not receive the OnItemClickListener anymore. Am i making sense ? I mean after the menu disappears, the same item does not receive the click listener. The items above and below receive the event as desired. I seems as if the Menu has disappeared but still is catching the click event ?
It's bad. You have to call registerForContextMenu in onCreate method.
So try it like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
registerForContextMenu(<yourListView>);
...
}
for create ContextMenu you have to override onCreateContextMenu method
#Override
public void onCreateContextMenu(ContextMenu cMenu, View parent, ContextMenu.ContextMenuInfo info) {
this.contextMenu = cMenu;
new MenuInflater(Contacts.this).inflate(R.menu.conmenu, this.contextMenu);
}
and for select items override onContextItemSelected method:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.c_odobrat:
deleteContactDialog(info.id);
return true;
}
return false;
}
And it should works.
quickLinkListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent intent = new Intent();
intent.setClassName(Home.this, "com.myapp.toc" + Constants.ACT_NAMES[position]);
if (position < 4 && position > 1)
{
switch (position)
{
case 3:
registerForContextMenu(v);
ViewHolder.v=v;
openContextMenu(v);
break;
}
}
}
});
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_home, menu);
menu.setHeaderTitle("Select Video Type");
}
I've got a ListActivity with a onClickListener which succesfully captures the item's position index which I then use to grab objects of my feeding ArrayList of Document objects. I would also like to be able to grab this position int when opening a Context Menu. How do I do that? Code snippets:
My successfuly working onListItemClick
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
SearchResult sr = searchResults.get(position); // this is how I grab my document
Toast.makeText(this, sr.toString(), Toast.LENGTH_SHORT).show();
}
Is there a way to grab this integer by these methods?
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
OR
public boolean onContextItemSelected(MenuItem item) {
Thanks guys!
Something like this:
int lastPosition = -1;
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) menuInfo;
lastPosition = info.position;
}
public boolean onContextItemSelected(MenuItem item) {
doStuff(lastPosition);
}