简体   繁体   中英

Why in my app the methods getChild, getChildId and getChildView doesn't execute

I'm my app I've a trouble with expandable list view: when I try to run my app in debug mode, I see that the methods getChild, getChildId and getChildView aren't execute and I don't know why. I understand that this methods will execute after tapping on a group item, but in my case this never happen. I used this adapter:

ExpandableListAdapter.java

package it.bitmama.interactv4.adapter;

import it.bitmama.interactv4.R;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Activity context;
    private Map<String, List<String>> episodeData;
    private List<String> episodes;

    public ExpandableListAdapter(Activity context, List<String> episodes, Map<String, List<String>> episodeData) {
        this.context = context;
        this.episodeData = episodeData;
        this.episodes = episodes;
    }

    public Object getChild(int groupPosition, int childPosition) {
        Log.d("getChild", "" + episodeData.get(episodes.get(groupPosition)).get(childPosition));
        return episodeData.get(episodes.get(groupPosition)).get(childPosition);
    }

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

    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        Log.d("getChildView", "getChildView");
        final String episode = (String) getChild(groupPosition, childPosition);
        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_item, null);
        }

        TextView item = (TextView) convertView.findViewById(R.id.episodeTime);

//        ImageView add = (ImageView) convertView.findViewById(R.id.addCalendar);
//        add.setOnClickListener(new OnClickListener() {
//            public void onClick(View v) {
//                Log.d("EVENTI", "Aggiungere al calendario");
//
//                Calendar cal = Calendar.getInstance();
//                Intent intent = new Intent(Intent.ACTION_EDIT);
//                intent.setType("vnd.android.cursor.item/event");
//                intent.putExtra("beginTime", cal.getTimeInMillis());
//                intent.putExtra("allDay", false);
//                intent.putExtra("endTime", cal.getTimeInMillis() + 60 * 60 * 1000);
//                //intent.putExtra("title", title);
//                context.startActivity(intent);
//
//            }
//        });
        item.setText(episode);
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        Log.d("getChildCount", "" + episodeData.get(episodes.get(groupPosition)).size());
        return episodeData.get(episodes.get(groupPosition)).size();
    }

    public Object getGroup(int groupPosition) {
        Log.d("getGroup", "" + episodes.get(groupPosition));
        return episodes.get(groupPosition);
    }

    public int getGroupCount() {
        Log.d("getGroupCount", "" + episodes.size());
        return episodes.size();
    }

    public long getGroupId(int groupPosition) {
        Log.d("getGroupId", "" +groupPosition);
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Log.d("getGroupView", "getGroupView");
        String episodeName = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_item,
                    null);
        }
        TextView item = (TextView) convertView.findViewById(R.id.episodeName);
        item.setTypeface(null, Typeface.BOLD);
        item.setText(episodeName.substring(0, 25));
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

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

You can see that I use a Map in which there are the information for the child and the information are in this Map I checked that by using Log.d (you can see here that in episodeDate there are this information: EpisodeData内容 ). What you think it's wrong? I tried lot of solution without any success. I hope that you will be able to help me to fix this issue. If you need the child_item.xml you can ask me and I will post here.

Thank you!

PS: in my code there are comment because next step it's to put an event to the smartphone/tablet calendar, but before do that I will solve the problem.

EDIT: In the activity who needs to create the expandable list view I made so:

final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(this, groupList, episodeData);
exPrograms.setAdapter(expListAdapter);

The other parts of my code you can see in the code I posted before this edit.

I experienced the same problem which was caused by not setting the view in the activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    view = new ExpandableListView(this);

    // do your stuff

    // set it as the view
    setContentView(view);

}

Apparently, if you forget to setview as the contentview Android still calls the group methods and also asks for the number of children by caling getChildrenCount but then doesn't proceed with the other calls like getChild, getChildId, ... Very confusing.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM