2013-10-11 89 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])); 

你的代码看起来像this.-

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

为了更换两个switch句子,该阵列将是一个多一点confusing.-

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

然后你的整个开关句子CES可以只由一个line.-更换

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

我想你错过了点... – codeMagic

+0

绝对codeMagic。我只是想要另一个算法,而不是用“开关盒”来调用它们。 –

2

您可以自定义的类来存储您的物品标签和活动负责管理该选择喜欢的toString允许你按照你与治疗相同的行为串。

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

然后你就可以用这个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)); 

,最后内部

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

非常感谢你@Jansel的魅力。我有200多个活动,现在我可以让他们在一个单一的代码中打开它们。谢谢再次为这样一个美妙的帮助。 –

+0

对于比上述复杂体系结构更好的实现。 – Eefret

相关问题