Hi want to know can we get the value of a field defined in the list like id from database in
onContextItemSelected()? my code for creating context menu is pinning below, help appreciated, thnx
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long clickedPosition = info.id;
switch (item.getItemId()) {
case NEW_MENU_ITEM:
//callActivity(1);
break;
case SAVE_MENU_ITEM:
//callActivity(2);
break;
}
return super.onContextItemSelected(item);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderTitle("Options");
menu.add(0, NEW_MENU_ITEM, 0, "Delete");
menu.add(0, SAVE_MENU_ITEM, 1, "Rename");
}
the following code not retiring my id from database
long clickedPosition = info.id;
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Object obj = getListView().getItemAtPosition(info.position);
String name = obj.toString();
}
The value returned in that field depends entirely on your adapter implementation. It would be the same id that would be passed through to an OnItemClickListener, which is the value returned by getItemId() on your adapter.
If you are using CursorAdapter or a variant of that to supply the data to the list, then the _id column is the value returned by default, unless you have modified this method. For an ArrayAdapter, this is not the case and you would have to add the code to return the ID you need. If you need to pass back more complex data than just a single int, you could set a tag on the list item views with setTag() and retrieve it from the ContextMenuInfo since targetView is one of the parameters it carries.
You can retrieve the position of the element in this list by adding
int id= info.position
in onContextItemSelected;
Hope it helps
Set database_id as tag to your lisviewItem view.
view.setTag(database_id );
And get database_id from onContextItemSelected(MenuItem item) as
#Override
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int listPosition = info.position;
switch (item.getItemId()) {
case call:
View view = info.targetView;
String database_id=(String) view.getTag();
return true;
case defaullt:
return true;
}
return super.onContextItemSelected(item);
}
Related
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/
Already had the ability to delete a listview item using an onItemLongClick method but I'd rather use a floating context menu to do this.
Below is the code I currently have for the floating context menu. I followed the documentation which helped me set it up and then tried to search for a similar example to what I'm doing but couldn't find anything appropriate.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.payments_context, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete:
return true;
default:
return super.onContextItemSelected(item);
}
}
This is the code I had to delete the items in my listview before I decided to switch to a floating context menu
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
{
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
return false;
}
If someone could advise me on how to get this floating context menu working I'd be really grateful. I don't have the edit method done just yet, it's what I have to do after I get this completed.
If I understand correctly, you can get the index of the item in the ListView at the click position by using the following code:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
Using position, you can reuse the code of onItemLongClick pretty much as is:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete: {
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
}
return true;
default:
return super.onContextItemSelected(item);
}
}
You might want to look at the answer to this question.
I have an expandListView diaryDisplaylist
this is adapter DiaryExpandListAdapter mAdapterForAll which will be used to fill the diaryDisplayList
code snippet which does this:
mAdapterForAll = new DiaryExpandListAdapter(this, diaryList);
diaryDisplaylist.setAdapter(mAdapterForAll);
diarylist is of type:
diaryList = new ArrayList<Object>();
diaryList will have all items from about 8 databases from which expandlist is grouped according to date of the time of entry present in the database.
It displays it perfectly.
Now i have expandlist full of elements belonging to different databases.
So for edit and delete functionality i have used
diaryDisplaylist.setOnCreateContextMenuListener(this);
code snippet for contextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
menu.add(0, Constants.MENU_EDIT, 0, "Edit");
menu.add(0, Constants.MENU_DELETE, 1, "Delete");
}
}
#Override
public boolean onContextItemSelected(android.view.MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case Constants.MENU_EDIT:
// once i get item belonging to respective database i can edit
return true;
case Constants.MENU_DELETE:
return true;
default:
return super.onContextItemSelected(item);
}
}
In on contextMenuItem selected how can i find which database that item belongs to?
I cannot use adapter to find me the object because i dont have groupPosition and childPosition in contextItemSelected.
If i could get those i could have simply used.
Object selectedObject = mAdapterForAll.getChild(groupPosition, childPosition);
then using selectedObject i could have found out
if(selectedObject isInstanceOf db1)
to get group and child position i used
Object obj = diaryList.get((int) info.packedPosition);
in onContextItemSelected it worked.
Sorry the above code was buggy this code works fine for me to determine the group and child position from expandlist view
int groupPos = 0, childPos = 0;
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
childPos = ExpandableListView
.getPackedPositionChild(info.packedPosition);
}
I am using this code to get the Clicked Item position on the Context Menu:
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
}
as suggested here:
Android: How to find the position clicked from the context menu
But i get NullPointerException at line:
int index = info.position;
why is it so?
EDIT
I have 2 registered Views for the contextMenu like this:
button1.onClick(view v){
registerForContextMenu(v);
openContextMenu(v);
}
button2.onClick(view v){
registerForContextMenu(v);
openContextMenu(v);
}
then depending on v.getId() i populate the menu.
You can use directly:
EDITED BEGIN
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
// If the menu object can't be cast, logs an error.
Log.e("MENU", "bad menuInfo", e);
return;
}
position = sectionAdapter.getIndexForPosition(info.position);
menu.setHeaderTitle("");
String[] menuItems = { "item1","item2","item3","item4" };
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
EDITED END
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int position = item.getItemId();
switch (position)
{
case 0:
break;
.
.
.
default:
break;
}
}
Hope it will help you.
See this tutorial http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/
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