Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ExpandableListView and onChildClick not working

I've been reading a lot of threads with the same problem that I have, but I can't figure out what's happening. I have a expandable list view, but I can't make the onChildClickListener to work. The onGroupClickListener is working ok, but I dont really need it. I looked at a lot of examples and they all seem to be as mine. Here is the code:

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ListView;
import android.widget.Toast;

import org.ektorp.CouchDbConnector;
import org.ektorp.DbAccessException;
import org.ektorp.android.util.EktorpAsyncTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



import android.widget.TextView;



public class MainActivity extends Activity {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 EktorpAsyncTask getUsersTask = new EktorpAsyncTask() {


            @Override
            protected void doInBackground() {
                //here i connect to de BD and fill the Group and Children arrays                
            }


@Override
                protected void onSuccess() {

                UserArrayExpandableAdapter adapter = new UserArrayExpandableAdapter(getApplicationContext(),parentsChilds.getParents(), parentsChilds.getChilds());
ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.userview);
                expandableList.setAdapter(adapter);
                expandableList.setClickable(true);

                expandableList.setOnGroupClickListener(new OnGroupClickListener(){
                     @Override
                        public boolean onGroupClick(ExpandableListView arg0, View arg1,
                            int groupPosition, long arg3) {
                            return false;
                            }
                });

                expandableList.setOnChildClickListener(new OnChildClickListener()
                  {
                      @Override
                      public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
                      {
                          Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
                          return false;
                      }
                  });

Child view:

<?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:layout_marginTop="10dp"
android:orientation="vertical" 
android:clickable="true"
android:focusable="true">


<TextView
    android:id="@+id/day"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/DarkSeaGreen"
    android:paddingLeft="8dp"
    android:paddingBottom="4dp"
    android:textColor="@color/White"
    android:textSize="18sp"
    android:textStyle="bold" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" 
    android:clickable="true"
    android:focusable="true">

    <TextView
        android:id="@+id/subject"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:textStyle="bold" 
        android:textSize="16sp" />
</RelativeLayout>

</LinearLayout>

Group 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="fill_parent"
android:orientation="vertical"
android:paddingBottom="5dp" >

<TextView
    android:id="@+id/monthText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="26sp"
    android:background="@color/SkyBlue"
    android:textColor="@color/White"
    android:textStyle="bold" />

</LinearLayout>

MainActivity layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/userview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>



</RelativeLayout>

Adapter code:

public class UserArrayExpandableAdapter extends BaseExpandableListAdapter {
private final Context context;
private final ArrayList<ArrayList<EventVO>> childs;
private ArrayList<String> parents;

public UserArrayExpandableAdapter(Context context, ArrayList<String> parents,ArrayList<ArrayList<EventVO>> childs) {
    this.context = context;
    this.childs = childs;
    this.parents = parents;
}

@Override
public Object getChild(int arg0, int arg1) {
    return childs.get(arg0).get(arg1);
}

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

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    List<EventVO> child = (ArrayList<EventVO>) childs.get(groupPosition);


    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = inflater.inflate(R.layout.user_view, parent, false);
    }
    TextView nameView = (TextView) convertView.findViewById(R.id.name);
    TextView dateView = (TextView) convertView.findViewById(R.id.day);
    TextView subjectView = (TextView) convertView.findViewById(R.id.subject);
    nameView.setText(child.get(childPosition).getName());
    String day = child.get(childPosition).getDate();
    day = DateTransformer.getDay(day);
    dateView.setText(day);
    subjectView.setText(child.get(childPosition).getSubject());
        return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return childs.get(groupPosition).size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    if (convertView==null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = inflater.inflate(R.layout.parent_view, parent, false); 
    }
    TextView monthView = (TextView) convertView.findViewById(R.id.monthText);
    monthView.setText(parents.get(groupPosition));
    return convertView;
}

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

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


}

Thank you very much.

like image 954
masual Avatar asked Oct 21 '22 22:10

masual


1 Answers

I had the same problem. My ExpandableListView's childs had two ImageViews and a TextView. Setting the property TextView:focusable to "false" in the layout file solved the issue. Strangely, the same code (without setting the focusable property) worked fine in API8 but when executed on API16 emulator, childs didn't respond to click.

like image 195
ldelgadochile Avatar answered Oct 24 '22 18:10

ldelgadochile