【问题标题】:Add click event to Expandable List View child while extending Activity instead of Activity在扩展 Activity 而不是 Activity 时将单击事件添加到可扩展列表视图子视图
【发布时间】:2013-06-21 10:34:34
【问题描述】:

我想添加 Expandable ListView 子项的点击事件。我不想扩展 Activity,因为我在下面添加了另一个 Listview。现在我想添加孩子的点击事件。

package com.example.events;

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

import com.example.events.GroupEntity.GroupItemEntity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnChildClickListener {
    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
    String URL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_mainactivity);
        prepareResource();
        initPage();

    }

    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 3; i++) {
            GroupEntity ge = new GroupEntity();
            ge.Name = "Group" + i;

            for (int j = 1; j < 4; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = "Child" + j;
                ge.GroupItemCollection.add(gi);
            }

            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        ExpandableListAdapter adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);

        mExpandableListView.setAdapter(adapter);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(getApplicationContext(), childPosition+"Clicked", Toast.LENGTH_LONG).show();
        return true;
    }

}

【问题讨论】:

  • 我不想扩展 Activity,因为我在这里添加了另一个 ListView ...我没有看到第二个 ListView。要加吗?

标签: android expandablelistview


【解决方案1】:

您忘记添加 onChildClick 以查看。试试这个:

private void initPage() {
    mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    ExpandableListAdapter adapter = new ExpandableListAdapter(this,
            mExpandableListView, mGroupCollection);

    mExpandableListView.setAdapter(adapter);
    mExpandableListView.setOnChildClickListener(this);
}

最好的祝愿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多