我创建了可扩展列表视图男性和女性体育。在男士和女子体育项目下,我们可以选择其中一项运动。我让我的孩子回复点击是什么让我回答像#34;棒球被点击。"。我的问题是在点击我的一项运动之后如何获得NewActivity(打开新页面)(例如,如果我点击棒球将为我打开新页面,我将能够显示上一场比赛的结果) 。 Here is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/mtfinal"
>
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:dividerHeight="1.5dp"
>
</ExpandableListView>
</LinearLayout>
MainActivity.java:
package com.example.athletic_project.java;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class MainActivity<View> extends ActionBarActivity {
ExpandableListView exv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent,
android.view.View v, int groupPosition, int childPosition,
long id) {
// TODO Auto-generated method stub
String itemclicked=MyAdapter.childList[groupPosition][childPosition];
Toast.makeText(MainActivity.this, itemclicked + " is clicked.", Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
MyAdapter.java:
package com.example.athletic_project.java;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
Typeface typeface;
static String []parentList = {"Men's Sports","Women's Sports"};
static String [][]childList = {
{
"Baseball","Basketball","Bowling","Cross Country","Golf","Soccer","Track & Field"
},
{
"Baseball","Basketball","Bowling","Cross Country","Golf","Soccer","Track & Field","Volleyball"
}
};
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return parentList.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/KGTribecaStamp.ttf");
TextView tv = new TextView(context);
tv.setText(parentList[groupPosition]);
tv.setPadding(45, 10, 10, 10);
tv.setTextSize(18);
tv.setTextColor(Color.BLUE);
tv.setTypeface(typeface);
return tv;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
typeface=Typeface.createFromAsset(context.getAssets(),"fonts/KGTribecaStamp.ttf");
TextView tv = new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(45, 10, 10, 10);
tv.setTextSize(15);
tv.setTextColor(Color.WHITE);
tv.setTypeface(typeface);
return tv;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
答案 0 :(得分:0)
以下是您开始新活动的方式:
Intent myIntent = new Intent(this.getActivity(), NewActivity.class);
this.getActivity().startActivity(myIntent);
this.getActivity().finish();
答案 1 :(得分:0)
使用子项单击侦听器和switch语句知道选择了哪个
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (groupPosition)
{
case 0:
switch (childPosition)
{
case 0:
Baseball();
break;
case 1:
//do something
break;
}
case 1:
switch (childPosition)
{
case 0:
football();
break;
case 1:
//do something
break;
}
}
return false;
}
private void baseball() {
Intent myIntent = new Intent(MainActivity.this, baseball.class);
startActivity(myIntent);
}
private void football() {
Intent myIntent = new Intent(MainActivity.this, football.class);
startActivity(myIntent);
} }); }
现在创建2类baseball.java和football.java
import android.app.Activity;
import android.os.Bundle;
public class football extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.football_activity);
}}
import android.app.Activity;
import android.os.Bundle;
public class baseball extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseball_activity);
}}
现在需要2 xml fill football_activity.xml和baseball_activity.xml 在创建这些后我们需要转到AndroidManifest
将这些添加到AndroidManifest
<activity
android:name=".baseball"
android:parentActivityName=".MainActivity" >
</activity>
<activity
android:name=".football"
android:parentActivityName=".MainActivity" >
</activity>
完成了! :)