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

}

4

3 回答 3

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>>();,并将子项目添加为

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

最后在setOnChildClickListener你只需要的方法里面是

       EntryList entry = listDataChild.get( groupPosition ).get( childPosition );
       startActivity( new Intent( context, entry.activity ) );
于 2013-10-11T18:33:58.710 回答
1

您可以将类存储在数组中Activity,并使用childPosition索引访问它们。

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

你的代码看起来像这样。-

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句子,数组会更混乱一些。-

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

然后你的整个切换句子可以只用一行代替。-

startActivity(new Intent(Chapter_1_full.this, activityClasses[groupPosition][childPosition]));
于 2013-10-11T18:22:21.157 回答
0

这应该就是你所需要的!

Intent YourIntent = new Intent(this, YourActivity.class);
startActivity(YourIntent);
于 2013-10-11T18:21:43.080 回答