I have a simple Expandable List View in a Fragment with only two levels of lists: parent and child.
I would like to deploy methods when clicking on the child, so that each child opens up a different activities.
Now, I am using a setOnChildClickListener on my "expListView" this way
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View view,
int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
Intent Open00 = new Intent(getActivity(), First.class);
getActivity().startActivityForResult(Open00, 0);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
case 1:
Intent Open01 = new Intent(getActivity(), Second.class);
getActivity().startActivityForResult(Open01, 0);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
}
case 1:
switch (childPosition) {
case 0:
Intent Open1 = new Intent(getActivity(), Popup1.class);
getActivity().startActivityForResult(Open1, 1);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
case 1:
Intent Open2 = new Intent(getActivity(), Popup2.class);
getActivity().startActivityForResult(Open2, 1);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
}
case 2:
switch (childPosition) {
case 2:
break;
case 3:
break;
}
}
return false;
}
});
What happens now, is that if I click on the first child (number 0) of the first group (number 0 again), it opens two activities, i.e. the number 0 from the group 0 and the 0th from the group 1.
I am wondering why, given that I have also used the switches to fix this issue.
Thanks in advance
I found it, jeez!
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
Intent Open00 = new Intent(getActivity(), First.class);
getActivity().startActivityForResult(Open00, 0);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
case 1:
Intent Open01 = new Intent(getActivity(), Second.class);
getActivity().startActivityForResult(Open01, 0);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
}
**break**;
case 1:
switch (childPosition) {
case 0:
Intent Open1 = new Intent(getActivity(), Popup1.class);
getActivity().startActivityForResult(Open1, 1);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
case 1:
Intent Open2 = new Intent(getActivity(), Popup2.class);
getActivity().startActivityForResult(Open2, 1);
getActivity().overridePendingTransition(R.anim.bounce, R.anim.bounce);
break;
}
break;
case 2:
switch (childPosition) {
case 2:
break;
case 3:
break;
}
**break**;
case 3:
switch (childPosition) {
case 0:
break;
case 1:
break;
}
**break**;
}
return false;
}
});
I forgot to enter the break at the end of the groupcase!
Related
I am using a navigation drawer and want it to do different activities for different cases. I am using the switch method to implement the activities. I have implemented 3 switch cases but however, it keeps doing the case 3 instead of case 2. Here is my code that i am using:
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Discover");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
}
return true;
})
.build();
You forgot to add break; at the end of each switch case.
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
break;
}
Hope this will help!!
ppl am trying to do menu option...and i got it,but after clicking the option in the slide menu...the control should go the respective page..i tried ..but its not working..please help me
switch(position){
case '0': {
startActivity(new Intent(home.this, home.class));
finish();
}
case '1':
{
startActivity(new Intent(home.this, info_values.class));
finish();
}
}
You should try like this
switch(position){
case 0:
startActivity(new Intent(home.this, home.class));
finish();
break;
case 1:
startActivity(new Intent(home.this, info_values.class));
finish();
break;
default:
break;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 'R.id.a':
startActivity(new Intent(home.this, home.class));
case 'R.id.b':
startActivity(new Intent(home.this, info_values.class));
}
return true;
}
In my application i don't want to recreate activity on navigation drawer menu click when i am on same activity.
user on map activity after navigation drawer item click map reloading. i don't want to reload map or refresh activity.
private void displayView(int position)
{
switch (position)
{
case 0:
break;
case 1:
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
break;
case 2:
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
break;
case 3:
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
break;
default:
break;
}
You can add the following to your function
private int current=-1;
private void displayView(int position)
{
switch (position)
{
case 0:
current =0;
break;
case 1:
if(current!=1){
current =1;
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
}
break;
case 2:
if(current!=2){
current=2;
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
}
break;
case 3:
if(current!=3){
current=3
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
}
break;
default:
break;
}
I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity
I have implemented the Sliding Menu in my App. Source: https://github.com/johnkil/SideNavigation
It works like it should but when I click on any item in my menu, the click wont work some reason. I added onClick listener and all that.
Code snippet:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
public void onSideNavigationItemClick(int itemId) {
switch (itemId) {
case R.id.side_navigation_menu_item1:
Toast.makeText(getApplicationContext(),( R.string.title1),
Toast.LENGTH_LONG).show();
break;
case R.id.side_navigation_menu_item2:
Intent intent = new Intent(this, DiffAdapter.class);
this.startActivity(intent);
break;
case R.id.side_navigation_menu_item3:
invokeActivity(getString(R.string.title3), R.drawable.ic_action_storage);
break;
case R.id.side_navigation_menu_item4:
invokeActivity(getString(R.string.title4), R.drawable.ic_action_settings);
break;
case R.id.side_navigation_menu_item5:
invokeActivity(getString(R.string.title5), R.drawable.ic_launcher);
break;
default:
return;
}
finish();
}
Any help would be nice. It just wont do anything when I click on a item.
Thanks
I forgot to implement some strings, which caused bugs. Got this solved by adding:
icon = (ImageView) findViewById(android.R.id.icon);
sideNavigationView = (SideNavigationView) findViewById(R.id.side_navigation_view);
sideNavigationView.setMenuItems(R.menu.ribbon_menu);
sideNavigationView.setMenuClickCallback(this);
if (getIntent().hasExtra(EXTRA_TITLE)) {
String title = getIntent().getStringExtra(EXTRA_TITLE);
int resId = getIntent().getIntExtra(EXTRA_RESOURCE_ID, 0);
setTitle(title);
icon.setImageResource(resId);
}
to my code: onCreate ()