Java 调用活动的另一种方法。?
这是我的代码。。 我有很多活动要切换,这段代码似乎工作得很好。但我想知道是否有另一种方法比“切换案例”方法更简单、更容易实现 在这里输入代码Java 调用活动的另一种方法。?,java,android,android-listview,onclicklistener,Java,Android,Android Listview,Onclicklistener,这是我的代码。。 我有很多活动要切换,这段代码似乎工作得很好。但我想知道是否有另一种方法比“切换案例”方法更简单、更容易实现 在这里输入代码 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.conten
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;
}
package com.prashant.dfs;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.os.Bundle;
导入android.app.Activity;
导入android.content.Intent;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ExpandableListView;
导入android.widget.ExpandableListView.OnChildClickListener;
公开课第1章全面展开活动{
可扩展列表适配器;
ExpandableListView解释视图;
列表列表数据头;
HashMapListDataChild;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u chapter\u 1\u full);
//获取列表视图(展开)
expListView=(ExpandableListView)findViewById(R.id.ch1\u expand);
//预备主义者
prepareListData();
listAdapter=新的ExpandableListAdapter(此,ListDataHeader,listDataChild);
expListView.setAdapter(listAdapter);
}
私有void prepareListData(){
ListDataHeader=新的ArrayList();
listDataChild=newHashMap();
//添加子数据
添加(“1.1算法简介”);
添加(“1.2数据结构”);
添加(“1.3 ADT”);
List Intro=new ArrayList();
介绍添加(“算法设计”);
引言添加(“流程图”);
添加介绍(“伪语言”);
List dataStructure=new ArrayList();
数据结构。添加(“数据结构类型”);
数据结构。添加(“原始和非原始”);
数据结构。添加(“线性和非线性”);
dataStructure.add(“静态和动态”);
List ADT=new ArrayList();
ADT.添加(“数据对象”);
listDataChild.put(ListDataHeader.get(0),Intro);
listDataChild.put(ListDataHeader.get(1),dataStructure);
listDataChild.put(ListDataHeader.get(2),ADT);
setOnChildClickListener(新的OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
最终对象childObj=parent.getExpandableListAdapter().getChild(groupPosition,childPosition);
if(childObj!=null)
{
开关(组位置)
{
案例0:
{
开关(儿童位置)
{
案例0:
{
startActivity(新意图(第1章完整,这是TestActivity.class));
打破
}
案例1:
{
startActivity(新意图(第1章完整,本,TestActivity2.class));
打破
}
案例2:
{
startActivity(新意图(第1章完整,本,TestActivity3.class));
打破
}
}
打破
}
案例1:
startActivity(新意图(第1章完整,本,TestActivity4.class));
}
}
返回true;
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单第1章已满,菜单);
返回true;
}
}这应该是您所需要的全部强>
Intent YourIntent = new Intent(this, YourActivity.class);
startActivity(YourIntent);
您可以将
活动
类存储在一个数组中,并使用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]));
您可以创建一个自定义类来存储项目标签和负责管理该选择的活动,如
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;
}
}
绝对是魔法。我只是想用另一个算法,用我的条件来调用它们,而不是用“开关盒”。我明白了这一点。稍微解释一下会更好。好吧,你只需要使用数组来跳过内部开关块。如果你试着在心里替换
childPosition
所取的值,你可以看到startActivity语句是如何等效的。谢谢你,两个答案都基于相同的逻辑。现在我可以继续使用我的应用程序了。再次感谢。非常感谢你@jansel,你的工作真有魅力。我有200多个活动,现在我可以在一个代码中打开它们。再次感谢您的帮助。对于复杂的体系结构,这比上面的实现要好得多。
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 ) );