gpt4 book ai didi

android - 在扩展 Activity 而不是 Activity 时向可扩展 ListView 子项添加点击事件

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:19 25 4
gpt4 key购买 nike

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

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

}

最佳答案

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

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

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

最良好的祝愿。

关于android - 在扩展 Activity 而不是 Activity 时向可扩展 ListView 子项添加点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com