1

我是我的应用程序我在使用可扩展列表视图时遇到了问题:当我尝试在调试模式下运行我的应用程序时,我发现 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;
    }
}

你可以看到我使用了一个地图,其中有孩子的信息,并且信息在这个地图中我通过使用 Log.d 进行了检查(你可以在这里看到,在 episodeDate 中有这个信息:)剧集数据内容。你觉得哪里不对?我尝试了很多解决方案但没有任何成功。我希望你能帮助我解决这个问题。如果你需要 child_item.xml,你可以问我,我会在这里发布。

谢谢!

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

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

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

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

4

1 回答 1

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 设置为 contentview,Android 仍会调用组方法,并且还会通过调用 getChildrenCount 来询问孩子的数量,但随后不会继续进行其他调用,例如 getChild、getChildId、... 非常令人困惑。

希望这可以帮助。

于 2013-09-30T19:03:43.107 回答