为什么在我的应用程序中,方法getChild,getChildId和getChildView不会执行

时间:2013-07-26 14:45:26

标签: android adapter expandablelistview

我是我的应用程序我在扩展列表视图中遇到了麻烦:当我尝试在调试模式下运行我的应用程序时,我看到方法getChild,getChildId和getChildView都没有执行,我不知道为什么。 我知道这个方法会在点击一个组项后执行,但在我的情况下,这种方法永远不会发生。 我用过这个适配器:

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

你可以看到我使用了一个Map,其中有孩子的信息,信息在这个Map中我通过使用Log.d检查过(你可以看到episodeDate中有这些信息:{{ 0}})。你认为这是错的?我尝试了很多解决方案而没有任何成功。 我希望你能帮助我解决这个问题。如果你需要child_item.xml,你可以问我,我会在这里发帖。

谢谢!

PS:在我的代码中有评论,因为下一步是将事件放到智能手机/平板电脑日历上,但在此之前我会解决问题。

编辑: 在需要创建可扩展列表视图的活动中,我这样做了:

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

我可以在编辑之前发布的代码中看到我的代码的其他部分。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这是由于未在活动中设置视图引起的:

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

}

显然,如果您忘记将setview设置为内容视图,Android仍会调用组方法,并通过调用getChildrenCount来询问子项数,但之后不会继续执行其他调用,如getChild,getChildId,......非常混乱

希望这有帮助。