Qandeel Abbassi
Qandeel Abbassi

Reputation: 999

handling click event on childview containing multiple views in expandable listview

I have expandable listview like this. I want to open another activity when user clicks on see description. I tried to set button listener in getchildview method of my custom expandable list adapter but i got to know that i can not use intent my adapter class. Also i cant use on child click (as far as i know) cuz i have multiple view in my childview. I am totally confused.... and have been trying for a day. i have googled alot but got no solution.

This is my custom adapter for my expandable list:

package com.qandeelabbasi.homeactivity;

import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, HashMap<String, String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, HashMap<String, String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public HashMap<String, String> getChild(int groupPosition,
        int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition));
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final String Ven = getChild(groupPosition, childPosition).get("venue");
    final String Dat = getChild(groupPosition, childPosition).get("date");

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_layout_exview,
                parent, false);
    }

    TextView txtListChild1 = (TextView) convertView
            .findViewById(R.id.lblListItem);
    txtListChild1.setText(Ven);

    TextView txtListChild2 = (TextView) convertView
            .findViewById(R.id.textView1);
    txtListChild2.setText(Dat);

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.parent_layout_exview,
                null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

And this is my childview layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/textVenue1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Venue:"
            android:textStyle="bold"
            android:typeface="sans" />

        <TextView
            android:id="@+id/lblListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingLeft="5dp"
            android:text=""
            android:typeface="sans" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/textDate1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Date:"
            android:textStyle="bold"
            android:typeface="sans" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:paddingLeft="15dp"
            android:text="12/05/2014"
            android:typeface="sans" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:weightSum="2" >

        <Button
            android:id="@+id/buttonJoin1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:maxHeight="40dp"
            android:minHeight="25dp"
            android:text="Join" />

        <Button
            android:id="@+id/buttonMaybe1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_style"
            android:maxHeight="40dp"
            android:minHeight="25dp"
            android:text="May be" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonSeeDescription"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@drawable/button_style"
        android:text="See Description" />

</LinearLayout>

Upvotes: 2

Views: 950

Answers (1)

user2965003
user2965003

Reputation: 334

Whats wrong with setting the onClickListener in getChildView()?

Button Bt_Name = (Button)convertView.findViewById(R.id.buttonSeeDescription);

Bt_Name.setOnClickListener(new OnClickListener(
                                               
    @Override        
    private void Onclick(View view){
                           
                           Intent intent = new Intent(_context, DestinationActivity.class);
                           _context.startActivity(intent);
                           
                           });

Upvotes: 2

Related Questions