0

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>
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • 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() { public void OnClick(android.view.View v) { // or any other key Object if( v.getTag() instanceof String ) { Toast.makeText(MainActivity.this, (String)v.getTag() + " is clicked.",Toast.LENGTH_SHORT).show(); } } })); } } – espresso_coffee Nov 10 '14 at 16:21

1 Answers1

0

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

}
HiXoid
  • 51
  • 3
  • I tried but did not work, can you explain one more time how tv.setOnClickListener suppose to look and tv.setTag? Thanks in advance. – espresso_coffee Nov 09 '14 at 15:03
  • Sorry, could not answer yesterday. Is the question still actual? – HiXoid Nov 10 '14 at 12:34
  • I create project based on your sources posted and it's work (show toasts with clicked item) so please clarify what additional behavior required. – HiXoid Nov 10 '14 at 15:18
  • Can you post that part how suppose to look? I might implement in the wrong part of my code. – espresso_coffee Nov 10 '14 at 15:23
  • I almost do not modify your code. Just remove background image, font assign and replace ActionBarActivity with Activity (to not use appcompat lib for test puproses). During remove font from your code I see one moment: do not create Typeface each time in getView, create it once in MyAdapter custructor. – HiXoid Nov 10 '14 at 15:27
  • This is what I created under getChildView: tv.setOnClickListener(itemClick); tv.setTag(childList); My question is where I have to create "itemClick" and how that suppose to look? – espresso_coffee Nov 10 '14 at 15:34
  • I made a changes and implemented your code but does not work... Update answer. – espresso_coffee Nov 10 '14 at 16:20
  • Try to run this [.apk](https://drive.google.com/file/d/0Bx1hsIY2pEejXzc5aUhXaFBmWjA/view?usp=sharing) on any API19 device, I test it on emulator. – HiXoid Nov 10 '14 at 16:23
  • This code what you posted does not make any changes in comparing with my code. I need with clicking to one of the child's to open new page. That was my point. – espresso_coffee Nov 10 '14 at 16:30
  • "new page" is new activity? `Intent intent=new Intent(context, SomeActivity.class); intent.putExtra(KEY_NAME, v.getTag()); startActivity(intent);` – HiXoid Nov 10 '14 at 16:35
  • That suppose to be under MyAdapter.java or MainActivity.java? – espresso_coffee Nov 10 '14 at 16:39
  • I tried but still does not create new page(Activity) just said that I clicked on specific sport. – espresso_coffee Nov 10 '14 at 20:56
  • It's complaining about KEY_NAME for some reason, and if I click on one of the sports my app stop working. – espresso_coffee Nov 10 '14 at 22:32