ListView - onContextItemSelected manipulate the item instead of toString - android

As you can see in the code below, when I LongClick on an item in ListView, i get a popup menu with option to manipulate the item (delete, update, etc).
the problem is that I use my function on item.toString instead the item itself.
How can i get the item itself, and put it as an argument in my functions?
onCreateContextMenu:
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.db_list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
onContextItemSelected:
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Object obj = lv.getItemAtPosition(info.position);
String nameToString = obj.toString();
if (item.getTitle().equals("Delete")) {
deletePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Update")) {
updatePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Change Host/Guest")) {
changeMembership(nameToString);
}
return true;
}

do like:
yourAdapter.getItem(info.position);
or
((YourAdapter)lv.getAdapter()).getItem(position);
or even simpler,
listOfItem.get(info.position);

Related

Delete row from sqlite (context menu)

What i want is when the user clic on contact a context Menu appear with item "delete" so i did this, but the problem is I want that when he clic on delete a row from Sqlit ( that corresponds to the contact I just clicked)
Activity.class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int i = info.position;
switch (item.getItemId()) {
case R.id.delete:
dbHandler.deleteEtudiant(i);
Toast.makeText(this,"deleted "+i, Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
MyDBHandler.class
public void deleteEtudiant(int i)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete("etudiant", "Id+ =" + i, null);
}
The problem with your code is that your are getting the id of "AdapterContextMenuInfo" object and sending it to the database "delete()" method. But actually you need to send the id of the of the item of database, the value that is stored in "ID" column of that particular row in SQlite Database.
Please have a look on the tutorial given below, specially check the "getContact()" and "deleteContact()" method of the tutorial.
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Using contextmenu with listview in android

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it?
Here is some code:
protected void onCreate(Bundle savedInstanceState) {
-------
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
And the onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
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.watch:
String name = "";
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
I want to get text from a textview in a list item.How do i achieve that?
To get the item from the ListView item selected refer to ContextMenuInfo object (see last implemented method below). Full solution as follows:
1) register ListView for context menu in ListActivity class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getListView().setAdapter(mAdapter);
registerForContextMenu(getListView());
}
1a) if you have complex View on your list you might need to enable long click on each list view in Adapter class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
itemLayout = layout;
itemLayout.setLongClickable(true);
}
// ...
return view;
}
2) implement onCreateContextMenu() and onContextItemSelected() in ListActivity class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
menu.setHeaderTitle(title);
menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_CONTEXT_DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Log.d(TAG, "removing item pos=" + info.position);
mAdapter.remove(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
The problem was the onItemLongClick() method, do not use it for context menu.
Instead, register the LISTVIEW for the context menu.
Here's the source.
for onCreate():
registerForContextMenu(lv);
And to access the selected item during long click:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
1) First we use
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("View Selected Text");
}
2)
list--is ref if ListView
registerForContextMenu(list);
3)
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(item.getTitle().equals("View Selected Text"))
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
Contact c=array.get(menuInfo.position);
Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
}
}
first get list using id
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
String packname = context.getPackageName();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
startActivity(LaunchIntent);
}
else {
Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
Write this in your longPressListener with the listview you use:
ListView list = (ListView) findViewById(android.R.id.list);
registerForContextMenu(list);
And this are the methods:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
Adapter adapter = getListAdapter();
Object item = adapter.getItem(info.position);
menu.setHeaderTitle("Choose");
menu.add(0, v.getId(), 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Delete") {
deleteContact(item.getItemId());
} else if (...) {
// code
} else {
return false;
}
return true;
}
public void deleteContact(int id){
// your code what to do for the clicked item
}
use these methods, onCreateContextMenu and onContextItemSelected\
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.listview) {
menu.setHeaderTitle("Delete");
menu.add(Menu.NONE, 0, 0, "Delete from list");
}
}
/**
* Responding to context menu selected option
* */
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
// check for selected option
if (menuItemIndex == 0) {
// user selected delete
// delete the listrow
..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
// reloading same activity again
Intent intent = getIntent();
finish();
startActivity(intent);
}
return true;
}
The above answers are very accurate and to the point for the case provided. That being said, I was brought here with using a convertView for my listview and am answering for those who are also brought here with this case.
If your LISTVIEW is using convertView and inflating a separate layout (say list_MyItem.xml), directly modify the list_MyItem.xml to have:
android:longClickable="true"
For example, if the listview is being populated with buttons modify the button as such:
<Button
android:id="#+id/myButton"
.
.
.
android:longClickable="true"
/>

android: listview long click & context popup causing stackoverflowerror (possibly due to actionbarsherlock?) [duplicate]

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it?
Here is some code:
protected void onCreate(Bundle savedInstanceState) {
-------
lv1 = (ListView) findViewById(R.id.listings);
registerForContextMenu(lv1);
lv1.setOnItemClickListener(this);
}
And the onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
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.watch:
String name = "";
return true;
case R.id.buy:
return true;
default:
return super.onContextItemSelected(item);
}
}
I want to get text from a textview in a list item.How do i achieve that?
To get the item from the ListView item selected refer to ContextMenuInfo object (see last implemented method below). Full solution as follows:
1) register ListView for context menu in ListActivity class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getListView().setAdapter(mAdapter);
registerForContextMenu(getListView());
}
1a) if you have complex View on your list you might need to enable long click on each list view in Adapter class
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
itemLayout = layout;
itemLayout.setLongClickable(true);
}
// ...
return view;
}
2) implement onCreateContextMenu() and onContextItemSelected() in ListActivity class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
menu.setHeaderTitle(title);
menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_CONTEXT_DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Log.d(TAG, "removing item pos=" + info.position);
mAdapter.remove(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
The problem was the onItemLongClick() method, do not use it for context menu.
Instead, register the LISTVIEW for the context menu.
Here's the source.
for onCreate():
registerForContextMenu(lv);
And to access the selected item during long click:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
1) First we use
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("View Selected Text");
}
2)
list--is ref if ListView
registerForContextMenu(list);
3)
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if(item.getTitle().equals("View Selected Text"))
{
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
Contact c=array.get(menuInfo.position);
Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
}
}
first get list using id
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
String packname = context.getPackageName();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
startActivity(LaunchIntent);
}
else {
Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
Write this in your longPressListener with the listview you use:
ListView list = (ListView) findViewById(android.R.id.list);
registerForContextMenu(list);
And this are the methods:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
Adapter adapter = getListAdapter();
Object item = adapter.getItem(info.position);
menu.setHeaderTitle("Choose");
menu.add(0, v.getId(), 0, "Delete");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Delete") {
deleteContact(item.getItemId());
} else if (...) {
// code
} else {
return false;
}
return true;
}
public void deleteContact(int id){
// your code what to do for the clicked item
}
use these methods, onCreateContextMenu and onContextItemSelected\
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.listview) {
menu.setHeaderTitle("Delete");
menu.add(Menu.NONE, 0, 0, "Delete from list");
}
}
/**
* Responding to context menu selected option
* */
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
// check for selected option
if (menuItemIndex == 0) {
// user selected delete
// delete the listrow
..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
// reloading same activity again
Intent intent = getIntent();
finish();
startActivity(intent);
}
return true;
}
The above answers are very accurate and to the point for the case provided. That being said, I was brought here with using a convertView for my listview and am answering for those who are also brought here with this case.
If your LISTVIEW is using convertView and inflating a separate layout (say list_MyItem.xml), directly modify the list_MyItem.xml to have:
android:longClickable="true"
For example, if the listview is being populated with buttons modify the button as such:
<Button
android:id="#+id/myButton"
.
.
.
android:longClickable="true"
/>

Android ListView ContextMenu not appearing

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");
}

How to change background color of each row in list view?

My app contains 1 list view, data source is 1 sqlite table, when i hold long click on any row in listview it will show me 1 menu option to change the color of that row, for this i have used onContextItemSelected function, on selecting menu option it will call 1 function change_color. What should i write in change_color function so that i can change row bg color.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color();
return true;
}
return super.onContextItemSelected(item);
}
Call your method as :
change_color(pass_your_list_view, pass_selected_position_of_list_view);
And define change_color() as:
private void change_color(ListView listView, int position) {
listView.getChildAt(position).setBackgroundColor(Color.BLACK);
}
Hope this will help.
Edited
Define a variable a position
public static int position;
And replace your code as
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// Retrieve the position at where you long pressed
position = info.position;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color(getListView(), position);
return true;
}
return super.onContextItemSelected(item);
}
refer to this tutorial Colored Row in List View

Categories

Resources