how to determine which class the item belongs to from onContextItemSelected - android

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

Related

Retrieve _id value without context menu

I am building a custom ListView (card style) and would like to have an edit and delete button on the card. In order to invoke the methods on the correct database rows, I need to know which _id (View) the user selected.
Currently, I am using a context menu which is pretty easy
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
position = (int) info.id;
MenuInflater m = getMenuInflater();
m.inflate(R.menu.history_popup_menu, menu);
}
#Override
public boolean onContextItemSelected(final MenuItem item) {
switch(item.getItemId()){
case R.id.delete:
...
in this case, I get the id (position) in the onCreateContextMenu
I am wanting to get rid of the context menu now and just have edit and delete on the card itself.
How do I go about retrieving the _id from the database and assign it to a value to be used in my delete/edit method?
This is my method for creating the list
private void addItemsToList(){
Cursor cursor = db.getAllLogs();
Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
String[] from = {"_id", "date", "gallons", "pricePerGallon", "odometer", "filledOrNot", "comments", "totalSpent"};
int[] to = {deleteVal, R.id.cardDate, R.id.cardGallons, R.id.cardPrice, R.id.cardOdometer, R.id.cardFilledOrNot, R.id.cardComments, R.id.usd};
adapter = new SimpleCursorAdapter(this, R.layout.history_list_item, cursor, from, to, 0);
String filledOrNotVal = String.valueOf(R.id.cardFilledOrNot);
if (filledOrNotVal == "False"){
Log.d("history", "False");
} else {
Log.d("history", "True");
}
listContent.setAdapter(adapter);
}

Deleting item from listview using floating context menu

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.

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"
/>

Get value from list on onContextItemSelected ()?

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

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