Developers Park
Developers Park

Reputation: 187

Android: Get Item from the navigation list actionbarsherlock

I have used actionbarsherlock library, in that i have used NavigationList and binded following data:

get Item from the navigation list:

Cursor cCategory = dbh.getCategory();

final String [] cName = new String[] {DatabaseHelper.CATEGORY_NAME};
int to[] = new int[]{R.id.listTextView};
Log.d("TAG", "CATEGORY NAME : "+cName.length);

//SIMPLE CURSOR ADAPTER
@SuppressWarnings("deprecation")
SimpleCursorAdapter categoryAdapter = new SimpleCursorAdapter(getActivity(), R.layout.textview_for_listview,cCategory, cName, to);
Log.d("TAG", "SIMPLE CURSOR ADAPTER  FOR CATEGORY : "+categoryAdapter.getCount());

// Hide the ActionBar Title
getSherlockActivity().getSupportActionBar().setDisplayShowTitleEnabled(false);
// Create the Navigation List in your ActionBar.
getSherlockActivity().getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);      

/* Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        Toast.makeText(getActivity(), "NAVIGATION CLICK :", 10).show();

        return false;
    }
};

/* Setting dropdown items and item navigation listener for the actionbar */
getSherlockActivity().getActionBar().setListNavigationCallbacks(categoryAdapter,navigationListener);

I want to get Item which is currently selected from Navigation List when i clicked on particular item onNavigationItemSelected?

Upvotes: 2

Views: 228

Answers (2)

Pratik Butani
Pratik Butani

Reputation: 62411

You can get item from your adapter as said by flx in answer but it will return Cursor.

so Your Listener like this:

ab.setListNavigationCallbacks(cityAdapter, new OnNavigationListener() {

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        // TODO Auto-generated method stub
        Cursor temp = (Cursor) categoryAdapter.getItem(itemPosition);

        Toast.makeText(context, temp.getString(temp.getColumnIndex("your_column_name")), Toast.LENGTH_LONG).show();

        return false;
    }
});

Upvotes: 2

flx
flx

Reputation: 14226

You can fetch the selected item from the adapter:

ActionBar.OnNavigationListener navigationListener =new OnNavigationListener() {
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        Toast.makeText(getActivity(). "selected item: " +
            categoryAdapter.getItem(itemPosition), 10).show();
        return false;
    }
};

Don't forget to mark the adapter as final. Otherwise you won't be able to access it from inside of the listener.

Upvotes: 0

Related Questions