ContextMenu for ListView

by Ankit Shah » Thu, 16 Jul 2009 03:48:12 GMT


Sponsored Links
 I have a ListActivity and using BaseAdapter i have populated ListItems
(Single ListItem row contains 3 views: 1 ImageView <"icon">, 1
TextView <"Title"> and 1 more TextView <"Description">).

Now When I short click on a list item, I get the position via
onListItemClick event. When I long click on the same listItem, the
context menu comes up. The thing is that there is no info on the
position (long click position/Index value).

I would like to get the selected item without requiring the user to
short click and then long click.

I have tried few things like getSelectedItem(), getSelectedItemId(),
and getSelectedItemPosition() but didn't work for me.

Help and Support would be greatly appreciated.

Thank You.
--~--~---------~--~----~------------~-------~--~----~



ContextMenu for ListView

by Mark Murphy » Thu, 16 Jul 2009 03:59:31 GMT


 


Sure there is.

Call getMenuInfo() on the MenuItem, and cast it to an
AdapterView.AdapterContextMenuInfo. You can then get the id of the item
that was clicked upon.

Here's a snippet of code from one of my book examples:

@Override
public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                case DELETE_ID:
                        AdapterView.AdapterContextMenuInfo info=
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

                        delete(info.id);
                        return(true);
        }

        return(super.onOptionsItemSelected(item));
}

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Looking for Android opportunities?  http://wiki.andmob.org/hado 

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


ContextMenu for ListView

by Francesco B. » Fri, 31 Jul 2009 02:58:06 GMT


 Hi, I'm interested in this topic. I have a question about the id that
returns from info.id. Is this id the same that is used by SQLite? As I
see in your example, that I suppose is taken from the developers
tutorial, you use info.id to delete an item from SQLite. But the
column in SQLite was named _id. Is this some kind of convention to
stick to? What if those items did not have an _id but a, suppose, _key
not called id? Could we do info.key?






--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Any api for mapview callout/bubble for tapped pins?

Hi,

There's no method available to us to display a callout / bubble above
an ItemizedOverlay item when, tapped, right? I don't think there is,
just wondering if I'm missing this in the docs before I start my own,

Thanks

-- 

2. Problem with AppWidget Using a Service

I am trying to create a simple AppWidget using a service to initialize
the content in the onUpdate() method.  The data is not being refreshed
and logcat shows me the following warning:

AppWidgetService  W  updateAppWidgetProvider: provider doesn't exist:
ComponentInfo{com.jnc.zmanminder/com.jnc.zmanminder.ZMUpdateService}

I must be missing something obvious but I cannot figure it out.

My AppWidget class (edited for brevity) looks as follows:

public class ZmanMinderAppWidget extends AppWidgetProvider {
  public void onUpdate(Context context,
    AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    context.startService(new Intent(context, ZMUpdateService.class));
  }
}

My Service class (edited for brevity) looks as follows:

public class ZMUpdateService extends Service {
  public void onStart(Intent intent, int startId) {
    RemoteViews updateViews = buildUpdate(this);
    ComponentName thisWidget = new ComponentName(this, ZMUpdateService.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, updateViews);
  }

  public IBinder onBind(Intent arg0) {
    return null;
  }
        
  public RemoteViews buildUpdate(Context context) {     
    Time time = new Time();
    time.setToNow();    
    RemoteViews views = new 
RemoteViews(context.getPackageName(),R.layout.widget);
    views.setTextViewText(R.id.time, time.format("%I:%M%p"));
    return views;
  }
}

The ZMUpdateService service is defined in my manifest file.

Thanks for any help.

...Jake


-- 
Jake Colman | Director, Software Development
Principia Partners LLC
101 West Elm Street | Conshohocken | PA 19428 | +1 (610) 755 9770
t: +1 (610) 755 9786 | c: +1 (610) 348 2788 | f: +1 (201) 221 8929
e: col...@ppllc.com | w: www.principiapartners.com
Credit technology innovation awards winner 2008 and 2009

-- 

3. Android 2.2, Stagefright, and HTTP Progressive Streaming

4. im3 acces

5. Porting android to the Palm TX

6. Galaxy S Rooted

7. playing simple tones