2

I am using MyExpandableListAdapter that extends BaseExpandableListAdapter and I implement all methods of BaseExpandableListAdapter in my adapter,I create MyExpadableListActivity that extends ExpandableListActivity. MyExpandableListAdapter is the inner class of MyExpadableListActivity. I am using a xml views for adding childs and groupd UI(past in below). As I run application its display fine. My problem is this this I override the method onChildClick (....) of ExpandableListActivity in MyExpadableListActivity .when I run my code it can't go for onChildClick method . how I can implement onChildClick (...) method using this situation or else suggestion .. Hope someone point me what I am doing wrong .... Be grateful for if anyone point how i can achieve solution . .

public class MyExpadableListActivity extends ExpandableListActivity implements
    ExpandableListView.OnChildClickListener {
    ExpandableListAdapter mAdapter;
    Context mContext;   
    static final String groups[] = { "Chats", "Contacts (289)",
        "Group Chat (7)", "e-Card  (137)", "Pending  (37)",
        "Bookmarks" };

static final String children[][] = {
        { "ListofChats" },
        { "Group", "groupt01", "groupt02", "groupt03" },
        { "Groupd 01", "", "Group  02", "", "Group  03", "" },
        { "E-Card-01", "", "E-Card-02", "",
                "Business Card Received", "" },
        { " Req", "", " Req", "", " Req", "" } };

private static final String TAG = "Hookup";
XMPPConnection connection = SignInMainActivity.connection;

@Override
public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);                 
    mAdapter = new MyExpandableListAdapter(this);
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());        
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    // TODO Auto-generated method stub
    System.out.println("Insidded onContextItemSelected");
    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Hookup Menu");
    menu.add(0, 0, 0, R.string.hello);
}

public boolean onContextItemSelected(MenuItem item) {
    System.out.println("Insidded onContextItemSelected");

    Log.i(TAG, "onContextItemSelected");
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
            .getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();
    int type = ExpandableListView
            .getPackedPositionType(info.packedPosition);

    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        int groupPos = ExpandableListView
                .getPackedPositionGroup(info.packedPosition);
        int childPos = ExpandableListView
                .getPackedPositionChild(info.packedPosition);
        Toast.makeText(this,title + ": Child " + childPos + " clicked in                    group " + groupPos, Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView
                .getPackedPositionGroup(info.packedPosition);
        Toast.makeText(this, title + ": Group " + groupPos + " clicked",
                Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    Context mContext;

    public MyExpandableListAdapter() {
        // TODO Auto-generated constructor stub
    }

    public MyExpandableListAdapter(Context context) {
        mContext = context;

    }

    public Object getChild(int groupPosition, int childPosition) {
        Log.i(TAG, "getChild");
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        Log.i(TAG, "getChildId");
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        Log.i(TAG, "getChildId");
        return children[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Log.i(TAG, "getChildView");

        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = null;

        TextView tt = null;
        if (groupPosition == 4) {
            v = layoutInflater.inflate(R.layout.button_group, null);
        } else {
            v = layoutInflater.inflate(R.layout.album_row, null);
            tt = (TextView) v.findViewById(R.id.text1);

            String myText = this.getChild(groupPosition, childPosition)
                    .toString();

            tt.setText(myText);
            CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
            cb.setVisibility(View.VISIBLE);

            if (groupPosition == 0) {
                ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
                icon.setImageResource(R.drawable.add_picture);
            } else {
                ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
                icon.setImageResource(R.drawable.add_picture);

            }
        }

                    return v;
    }

    public Object getGroup(int groupPosition) {
        Log.i(TAG, "getGroup");
        return groups[groupPosition];
    }

    public int getGroupCount() {
        Log.i(TAG, "getGroupCount");
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        Log.i(TAG, "getGroupId");
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView groupTitle = null;
        ImageView imgDot;
        LayoutInflater layoutInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = null;
        v = layoutInflater.inflate(R.layout.button_group, null);
        groupTitle = (TextView) v.findViewById(R.id.TextView01);
        String myText = this.getGroup(groupPosition).toString();
        groupTitle.setText(myText);
        imgDot = (ImageView) v.findViewById(R.id.ImageView01);
        imgDot.setVisibility(View.VISIBLE);
        return v;
    }
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.i(TAG, "isChildSelectable");
        return true;
    }
    public boolean hasStableIds() {
        Log.i(TAG, "hasStableIds");
        return true;
    }
    public void registerDataSetObserver(DataSetObserver observer) {
    }
} // closing of MyExpandableListAdapter 
} // closing of MyExpadableListActivity

My xml views are as below

<?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:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
android:paddingRight="?android:attr/scrollbarSize"
android:layout_weight="1" android:background="#fafafa">
<ImageView android:id="@+id/rowicon" android:layout_width="wrap_content"  
android:layout_height="fill_parent" android:layout_marginRight="6dip" /> <TextView   
android:id="@+id/text1"
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_weight="1"
/>
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:layout_marginRight="2dip" 
android:focusable="true" android:clickable="true" android:gravity="center_vertical" 
android:orientation="vertical" android:duplicateParentState="true" 
android:visibility="visible"
android:text="myTest"
/> </LinearLayout> 

and

<?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:minHeight="?android:attr/listPreferredItemHeight"   
android:gravity="center_vertical" 
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
android:paddingRight="?android:attr/scrollbarSize"
android:layout_weight="1" android:background="#fafafa">

Pasha
  • 2,407
  • 18
  • 25
aftab
  • 1,141
  • 8
  • 21
  • 40
  • Check this question: http://stackoverflow.com/questions/2367936/listview-onitemclicklistener-not-responding – Flo May 05 '11 at 11:52

3 Answers3

4

In the BaseExpandableListAdapter is one function: public boolean isChildSelectable(int groupPosition, int childPosition). This function returns Default false. Change it to: return true. And your onChildClickListener will be called !

Richard
  • 423
  • 3
  • 9
  • Dear Richard I solved my problem with other way.where I an creating dynamically expandable list without using xml layout that's working fine.if you read my code then you can see I also write as >public boolean isChildSelectable(int groupPosition, int childPosition) { Log.i(TAG, "isChildSelectable"); return true; } ,means I already do what you suggest.but now my second method working fine.. – aftab May 26 '11 at 15:38
  • hi mikedroid if you want solution i can give,as here in stackoverflow i am not ablve to past my complete code ..i will see any method if i can past my code here on this side .please ping me tonasirinbox(at)gmail(dot)com – aftab Jul 24 '11 at 12:18
2

you forgot register setOnChildClickListener(...), and it can't go for onChildClick( ) method.

Edit 1: You just need add one line in onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);                 
    mAdapter = new MyExpandableListAdapter(this);
    setListAdapter(mAdapter);
    getExpandableListView().setOnChildClickListener(this); //This should fix issue.
    registerForContextMenu(getExpandableListView());   //ContextMenu event override onChildClick and onGroupClick. Remove this line    
}

Edit 2: Try this link. And if it's not helped you, I advise you to not use BaseExpandableListAdapter. Use SimpleExpandableListAdapter, override 4 methods: getChildView(), getGroupView(), getChildrenCount() and getGroupCount(). And all should be good.

And do not forget to write comments. If this does not help, I will try to find more ideas.

Pasha
  • 2,407
  • 18
  • 25
  • hi @Pasha thanks for Quick reply,but my problem still here.As you suggest I use this ExpandableListView list = this.getExpandableListView(); int childCount = list.getChildCount(); System.out.print("Child Count" + childCount); list.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { System.out.print("Testing"); // TODO Auto-generated method stub return false; } }); – aftab May 05 '11 at 12:35
  • I also follow the comments given on this site link http://stackoverflow.com/questions/2512436/how-to-handle-android-simpleexpandablelistadapter-entry-selection , but same problem control still not coming inside of onChildClick Method . – aftab May 05 '11 at 13:07
  • @Pasha now I try example code given on http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html and do this setListAdapter(mAdapter); getExpandableListView().setOnChildClickListener(this); registerForContextMenu(getExpandableListView()); and override method @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id){ System.out.print("This is the test"); return false; } .but still control not come inside of onChildClick ...tired what I ma doing wrong.. – aftab May 05 '11 at 14:29
  • @Pasha not yet .I also try with code given on android developer site as i post in my yesterday comment but still control not come insdie of onChildClick, – aftab May 06 '11 at 07:57
  • @Pasha one of thing what I find that i was doing wrong with ExpandableList1.java (given example on android developer site what you mention in link in your last comment) was this I was putting super.onCreate(savedInstanceState); at top of onCreate method that was wrong when I code super.onCreate(savedInstanceState); line at the bottom of method it works fine.same thing I do with my above given issue but it still not working. – aftab May 07 '11 at 12:15
  • @Pasha thanks I got the solution and its working fine .now my expandablelistview totally dynamically ,I can add anything on it ...Thanks to stackoverflow developer and community members ... – aftab May 15 '11 at 09:28
0

I have tried implementing the Android ExpandableListView using BaseExpandableListAdapter in Kotlin.

I am not posting the XML code for layout.

MAINACTIVITY in Kotlin:

import android.app.Activity
import android.os.Bundle
import android.widget.ExpandableListAdapter
import android.widget.ExpandableListView
import java.util.ArrayList

class expandable_sticky_kotlin: Activity() {
     var listAdapter: ExpandableListAdapter? = null
     var exp_view: ExpandableListView? = null
     var list: MutableList<String>? = null
     val childList:HashMap<String,List<String>>?=null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_expandable_sticky_header)
        reloadData()
        exp_view = findViewById(R.id.lvExp)
        listAdapter = ExpandableAda(this,list,childList)
        exp_view!!.setAdapter(listAdapter)

    }

    fun reloadData()
    {
        list!!.add("top250")
        list!!.add("nowShowing")
        list!!.add("comingSoon")

        // Adding child data
        val top250 = ArrayList<String>()
        top250.add("The Shawshank Redemption")
        top250.add("The Godfather")
        top250.add("The Godfather: Part II")
        top250.add("Pulp Fiction")
        top250.add("The Good, the Bad and the Ugly")
        top250.add("The Dark Knight")
        top250.add("12 Angry Men")
        top250.add("Dark knight")
        top250.add("POTC")

        val nowShowing = ArrayList<String>()
        nowShowing.add("The Conjuring")
        nowShowing.add("Despicable Me 2")
        nowShowing.add("Turbo")
        nowShowing.add("Grown Ups 2")
        nowShowing.add("Red 2")
        nowShowing.add("The Wolverine")
        nowShowing.add("Deadpool2")
        nowShowing.add("Raazi")


        val comingSoon = ArrayList<String>()
        comingSoon.add("2 Guns")
        comingSoon.add("The Smurfs 2")
        comingSoon.add("The Spectacular Now")
        comingSoon.add("The Canyons")
        comingSoon.add("Europa Report")
        comingSoon.add("Avengers4")
        comingSoon.add("Meri apni movie")

        childList!!.put(list!!.get(0), top250) // Header, Child data
        childList!!.put(list!!.get(1), nowShowing)
        childList!!.put(list!!.get(2), comingSoon)

    }
}

ADAPTER for ExpandableView:

import android.content.Context
import android.database.DataSetObserver
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ExpandableListAdapter
import android.widget.TextView
import java.util.concurrent.ConcurrentLinkedQueue

class ExpandableAda(val context:Context,val list:List<String>?,val Child:HashMap<String,List<String>>?) : BaseExpandableListAdapter()
{


    var mContext = context
       var mparent_list = list
    var mChild = Child


    override fun onGroupCollapsed(p0: Int) {
        println("collpased")
    }
    override fun isEmpty(): Boolean {
        return false
    }

    override fun registerDataSetObserver(p0: DataSetObserver?)
    {
        println("here")
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        println("here")
        if (p0!=null) {
            observers.add(p0)
            println("here")
        }
    }

    override fun getChild(p0: Int, p1: Int): Any {
        return mChild!!.get(mparent_list!!.get(p0))!!.get(p1)
    }

    override fun onGroupExpanded(p0: Int) {
       print("Expanded")
    }

    override fun getCombinedChildId(p0: Long, p1: Long): Long
    {
        return p0
    }

    override fun getGroupId(p0: Int): Long {

        return p0.toLong()
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getChildView(p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
        var title:String = getChild(p0,p1) as String
        var myTest:View = p4!!


            var infalInflater = this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            myTest = infalInflater.inflate(R.layout.list_item, null)
        var lblListItem:TextView = myTest!!.findViewById(R.id.lblListItem)
        lblListItem.text = title
        return myTest
    }

    override fun areAllItemsEnabled(): Boolean {
        return true
    }

    override fun getChildId(p0: Int, p1: Int): Long {
        return p1.toLong()//To change body of created functions use File | Settings | File Templates.
    }

    override fun getCombinedGroupId(p0: Long): Long {
        return p0
    }

    override fun getGroupView(p0: Int, p1: Boolean, p2: View?, p3: ViewGroup?): View {
    var title:String = mparent_list!!.get(p0)
        var myTest:View = p3!!

            var inflater = this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        myTest = inflater.inflate(R.layout.list_group, null)
          var lblListHeader:TextView = myTest!!.findViewById(R.id.lblListHeader)

        lblListHeader.text = title
        return myTest!!
    }

    override fun unregisterDataSetObserver(p0: DataSetObserver?) {
        val observers = ConcurrentLinkedQueue<DataSetObserver>()
        if (p0!=null)
        {
            observers.remove(p0)
        }
    }

    override fun getGroupCount(): Int {

        return mparent_list!!.size
    }

    override fun getGroup(p0: Int): Any {
        return mparent_list!!.get(p0)
    }

    override fun getChildrenCount(p0: Int): Int {
        return mChild!!.get(mparent_list!!.get(p0))!!.size
    }

}
Ashutosh Shukla
  • 358
  • 5
  • 14