Android:从导航列表actionbarsherlock获取项目

时间:2014-02-12 04:46:35

标签: android android-actionbar

我使用了 actionbarsherlock 库,因为我使用了NavigationList并绑定了以下数据:

从导航列表中获取项目:

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

当我点击特定项目Item时,我想获得当前从Navigation List中选择的onNavigationItemSelected

2 个答案:

答案 0 :(得分:2)

您可以按照flx answer中{{3}}的说明从适配器获取项目,但它会返回Cursor。

所以你的Listener是这样的:

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

答案 1 :(得分:0)

您可以从适配器中获取所选项目:

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

不要忘记将适配器标记为final。否则,您将无法从侦听器内部访问它。