0

Refer on this link, I try to make 1 activity for many layout, but I've got a problem on how to implementing if else condition in my activity.

This is slice of my code :

TabRouting.java

public class TabRouting extends Fragment {

    .................

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            if (groupPosition == 0){
                if (childPosition == 0){
                    Intent a = new Intent(getActivity(), Content.class);
                    startActivity(a);
                }
                if (childPosition == 1){
                    Intent a = new Intent(getActivity(), Content.class);
                    startActivity(a);
                }
                if (childPosition == 2){
                    Intent a = new Intent(getActivity(), Content.class);
                    startActivity(a);
                }
                if (childPosition == 3){
                    Intent a = new Intent(getActivity(), Content.class);
                    startActivity(a);
                }
            }
            ................

            return false;
        }
    });

    ...............

    return v;

}

I want to make 1 activity (Content.java) to have many layout, so every I click on every child item of ExpandableListView I can see different layout.

Thanks :)

Community
  • 1
  • 1
ardhiart
  • 5
  • 2

1 Answers1

0

Try below code,

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
        int childPosition, long id) {
    Intent a = new Intent(getActivity(), Content.class);
    a.putExtra("position",childPosition);
    startActivity(a);

    ................

    return false;
}

Here you can pass the position to Content class and based on childPosition you can inflate different layout.

public class Content extends AppCompatActivity {

    @Override
    public void onCreate(Bundle bundle) {
        Intent intent = getIntent();
        int childposition = intent.getIntExtra("position",-1);
        switch(childposition) {
            case 0:
                setContentView(R.layout.layoutzero);
                break;
            case 1:
                setContentView(R.layout.layoutone);
                break;
             ........
        }
    }
}
Ashwini
  • 245
  • 1
  • 7
  • I got error when using `putInt` statement, so I replace it with `putExtra`, but I got error on my Content.java cause Intent can't be applied to String. – ardhiart May 24 '16 at 04:07
  • @ardhiart : I have edited my answer. You can check it now. – Ashwini May 24 '16 at 05:52
  • For 1 Group List that help enough, but how 'bout `groupPosition` if I want to add more Group List? – ardhiart May 24 '16 at 06:25
  • Similar way you can send group position too in intent and include switch case for group position also `switch(groupposition) { switch(childposition) { case 0 : break; ............... } }` – Ashwini May 24 '16 at 06:51
  • Ah, okok, I got it! Thanks! – ardhiart May 24 '16 at 08:01