I'm attempting to implement a toast depending on the item clicked in a DrawerLayout - however I cannot seem to get the toasts to appear no matter what I do. I'm not sure exactly what I did wrong: any suggestions are greatly appreciated.
private DrawerLayout drawerLayout;
private ListView drawerListView;
private String[] drawerListViewItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
drawerListViewItems = getResources().getStringArray(R.array.items);
drawerListView = (ListView) findViewById(R.id.left_drawer);
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
mPager.setOnPageChangeListener(mPageChangeListener);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
if ( position == 1 )
{
Toast.makeText(Home.this, "Item 1", Toast.LENGTH_LONG).show();
}
else if ( position == 2 )
{
Toast.makeText(Home.this, "Item 2", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Home.this, "Item 3", Toast.LENGTH_LONG).show();
}
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
drawerLayout.closeDrawer(drawerListView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.d("pk", "onItemClick");
// Highlight the selected item, update the title, and close the
// drawer
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
setTitle("......");
String text = "menu click... should be implemented";
// Toast.makeText(Home.this, text, Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}