I'm working on my project where I created Expandable List View with Men's and Women's sports. Each of them expands the list with sports like soccer, baseball, basketball, volleyball, golf. Now I'm at the point where I have to click on the one of the sports what gonna open new page, on that page will be presented previous games with results and upcoming events for each sport. All information's will be fetched from data base form athletic website. I do not how to make new page for individual sports with all information's what should be presented. Here is my code. Thanks in advance! Here is my code:
1)**MainActivity.java**
package com.example.athletic_project.java;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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;
}
});
}
}
2)**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;
}
}
3)**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>
In addChildView add onClickListener like tv.setOnClickListener(itemClick)
itemClick field of adapter. Initilize it from constructor (may be null of no clicks expected)
Also set to view tag
tv.setTag
Later in click listener get tag as key of pressed item and start your activity with this extra in intent to fetch appropriate data from DB to show
Like this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this, new OnClickListener() {
@Override
public void onClick(android.view.View v) {
// or any other key Object
if( v.getTag() instanceof String ) {
startPageActivity( (String)v.getTag());
}
}
}));
}
private void startPageActivity(String key) {
Intent intent=new Intent(MainActivity.this, SomeActivity.class);
intent.putExtra(KEY_NAME, key);
startActivity(intent);
}
public MyAdapter(Context context, OnClickListener listener) {
this.listener=listener;
this.context=context;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(45, 10, 10, 10);
tv.setTextSize(15);
tv.setTextColor(Color.BLACK);
// setup listener
tv.setOnClickListener(listener);
// u may set any Object
tv.setTag(childList[groupPosition][childPosition]);
return tv;
}
public class SomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String stringKey = getIntent().getExtras().getString(MainActivity.KEY_NAME);
//do something with passed key - fro example get data from DB to show
Toast.makeText(this, stringKey + " in new activity", Toast.LENGTH_SHORT).show();
}
}