Я создал Расширяемый список Посмотреть Мужские и женские виды спорта. В рамках мужского и женского спорта мы можем выбрать один из видов спорта. Я получил, что мои дети отвечают на щелчок, что дает мне ответ, как «Бейсбол нажал». Мой вопрос в том, как я могу получить NewActivity (открыть новую страницу) после того, как я нажму на один из моих видов спорта (например, если я нажму на бейсбол, который откроет для меня новую страницу, где я смогу показать результаты предыдущей игры) , Here is my code:
Как создать NewActivity для ребенка из ExpandableListView?
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;
}
}
У меня есть вопрос, я пробовал этот код и каждый раз, когда я нажимаю на Baseball, мое приложение перестает работать. Если я нажму на любой другой вид спорта, ничего не случится. Знаете, что случилось? –