2013-10-11 2 views
1

여기 내 코드는 .. 사이에 전환 할 수있는 많은 작업이 있고이 코드는 완벽하게 작동하는 것 같습니다. 그러나 "SWITCH CASE"방법보다는 구현하기가 덜 복잡하고 쉬운 다른 방법이 있는지 궁금합니다.활동을 호출하는 다른 방법.?

이 모든 당신이 필요로해야한다 여기

package com.prashant.dfs; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 

public class Chapter_1_full extends Activity { 


ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> ListDataHeader; 
HashMap<String,List<String>> listDataChild; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chapter_1_full); 

    //get the listView(expand) 
    expListView = (ExpandableListView) findViewById(R.id.ch1_expand); 
    //prepareDataList 
    prepareListData(); 

    listAdapter=new ExpandableListAdapter(this, ListDataHeader, listDataChild); 

    expListView.setAdapter(listAdapter); 
} 

private void prepareListData() { 
    ListDataHeader = new ArrayList<String>(); 
    listDataChild=new HashMap<String, List<String>>(); 
    //Adding child Data 

    ListDataHeader.add("1.1 Introductin to Algorithms"); 
    ListDataHeader.add("1.2 Data Structures"); 
    ListDataHeader.add("1.3 ADT"); 



    List<String> Intro = new ArrayList<String>(); 
    Intro.add("Algoithem Design "); 
    Intro.add("Flowcharting"); 
    Intro.add("Pseudo-Language"); 


    List<String> dataStructure = new ArrayList<String>(); 
    dataStructure.add("Type of Data Structure"); 
    dataStructure.add("Primitive and Non-Primitive"); 
    dataStructure.add("Linear and Non-Linear"); 
    dataStructure.add("Static and Dynamic"); 


    List<String> ADT = new ArrayList<String>(); 
    ADT.add("Datat Object");  

    listDataChild.put(ListDataHeader.get(0),Intro); 
    listDataChild.put(ListDataHeader.get(1),dataStructure); 
    listDataChild.put(ListDataHeader.get(2),ADT); 

    expListView.setOnChildClickListener(new OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 

      final Object childObj=parent.getExpandableListAdapter().getChild(groupPosition, childPosition); 

      if(childObj!=null) 
      { 
       switch (groupPosition) 
       { 
       case 0: 
       { 
        switch (childPosition) 
        { 
         case 0: 
         { 
          startActivity(new Intent(Chapter_1_full.this,TestActivity.class)); 
          break; 
         } 
         case 1: 
         { 
          startActivity(new Intent(Chapter_1_full.this,TestActivity2.class)); 
          break; 
         } 
         case 2: 
         { 
          startActivity(new Intent(Chapter_1_full.this,TestActivity3.class)); 
          break; 
         } 
        } 
        break; 
       } 
       case 1: 
        startActivity(new Intent(Chapter_1_full.this,TestActivity4.class)); 
       } 

      } 


      return true; 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.chapter_1_full, menu); 
    return true; 
} 

}

답변

1

당신은 배열에 Activity 클래스를 저장하고 인덱스로 childPosition를 사용하여 액세스 할 수 있습니다.

Class[] activityClasses = new Class[]{TestActivity1.class, TestActivity2.class, TestActivity3.class}; 
startActivity(new Intent(Chapter_1_full.this, activityClasses[childPosition])); 

당신은 코드가 배열이 조금 더 헷갈 것 모두 switch 문장을 대체하기 위해 이걸

switch (groupPosition) { 
    case 0: 
     startActivity(new Intent(Chapter_1_full.this, activityClasses[childPosition])); 
    break; 
    case 1: 
     startActivity(new Intent(Chapter_1_full.this,TestActivity4.class)); 
    break; 
} 

처럼 보일 것입니다

Class[][] activityClasses = new Class[][]{{TestActivity1.class, TestActivity2.class, TestActivity3.class}, {TestActivity4.class}}; 

그러면 전체 스위치가 센센 CES는 toString은 당신이 치료로는 다음과 같은 동작을 허용,

startActivity(new Intent(Chapter_1_full.this, activityClasses[groupPosition][childPosition])); 
+0

나는이 점을 조금씩 설명하고있다. –

+0

글쎄, 배열을 사용하여 내부 스위치 블록을 건너 뛰기 만하면됩니다. 'childPosition'이 취하는 값을 정신적으로 대체하려고하면 startActivity 문장이 어떻게 동등한 지 알 수 있습니다. – ssantos

+0

감사합니다. 두 답변 모두 같은 논리를 기반으로합니다. 이제는 내 앱으로 갈 수 있습니다. 다시 한 번 감사드립니다. –

0

코드를 입력!

Intent YourIntent = new Intent(this, YourActivity.class); 
startActivity(YourIntent); 
+1

됩니다 그럼 당신은이 listDataChild=new HashMap<String, List<EntryList>>();과지도를 대체 할 수 있고, 당신은 단지 필요 setOnChildClickListener 방법에 마지막으로 내부

 List<EntryList> Intro = new ArrayList<EntryList>(); Intro.add(new EntryList("Algoithem Design ", youractivity.class)); Intro.add(new EntryList("Flowcharting", youractivity.class); Intro.add(new EntryList("Pseudo-Language", youractivity.class)); 

로 하위 항목을 추가하고 point ... – codeMagic

+0

절대적으로 codeMagic. 나는 단지 "스위치 케이스 (switch case)"가 아닌 내 상태로 그들을 불러내는 또 다른 고고함을 원한다. –

2

당신은 당신에게 같은 그 선택을 관리 할 책임이 항목 라벨 및 활동을 저장하는 사용자 정의 클래스를 만들 수 있습니다 하나 말해봐에 의해 대체 될 수 끈.

public class EntryList{ 
    public String label;   
    public Class<?> activity; 

    public EntryList(String label,Class<?> activity){ 
     this.label =label; 
     this.activity = activity; 
    }  
    @Override 
    public String toString() {  
     return label; 
    } 
} 

난 당신이 놓친 생각

 EntryList entry = listDataChild.get(groupPosition).get(childPosition); 
     startActivity(new Intent(context, entry.activity)); 
+0

감사합니다. 매력처럼 작동하는 많은 @jansel. 나는 200 개 이상의 활동을하고 있으며 이제는 그들을 열어 단일 코드로 작업 할 수있게되었습니다. 다시 한번 감사드립니다. –

+0

위의 아키텍처보다 복잡한 아키텍처의 경우 훨씬 더 구현되었습니다. – Eefret

관련 문제