Java 单击侦听器与onchildclick类似,onitemlongclick在可展开的Listview中不起作用,GroupClickListener除外?
嗨,朋友们,我在可扩展列表视图中工作 我从数据库中获取值,它显示得非常好, 但是没有听众在里面工作 以下是我的资料来源 MainActivity.javaJava 单击侦听器与onchildclick类似,onitemlongclick在可展开的Listview中不起作用,GroupClickListener除外?,java,android,Java,Android,嗨,朋友们,我在可扩展列表视图中工作 我从数据库中获取值,它显示得非常好, 但是没有听众在里面工作 以下是我的资料来源 MainActivity.java package shenhengbin.practice; import java.util.ArrayList; import java.util.List; import shenhengbin.practice.entity.Constants; import shenhengbin.practice.entity.DBSQLite
package shenhengbin.practice;
import java.util.ArrayList;
import java.util.List;
import shenhengbin.practice.entity.Constants;
import shenhengbin.practice.entity.DBSQLite;
import shenhengbin.practice.entity.GroupEntity;
import shenhengbin.practice.entity.GroupEntity.GroupItemEntity;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ExpandableListView mExpandableListView;
private ListView listView;
private List<GroupEntity> mGroupCollection;
private BaseExpandableListAdapter adapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
setupDefaults();
setupEvents();
}
private void init() {
listView=(ListView) findViewById(R.id.listView);
mExpandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
}
private void setupDefaults() {
prepareResource();
}
private void setupEvents() {
adapter =new ExpandableListAdapter (this,mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.e("child","clicked");
return false;
}
});
mExpandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.e("Long","Click ENabled");
return false;
}
});
}
private void prepareResource() {
mGroupCollection = new ArrayList<GroupEntity>();
DBSQLite.openDatabase(getApplicationContext());
Cursor favoriteResultSet=Constants.SQLITE_CONNECTION.select("select * from names");
if(favoriteResultSet.getCount()>0)
{while(favoriteResultSet.moveToNext()){
String name =favoriteResultSet.getString(favoriteResultSet.getColumnIndex("names"));
Log.e("google_",""+name);
GroupEntity ge = new GroupEntity();
ge.Name = name;
Cursor favoriteResultSetChild=Constants.SQLITE_CONNECTION.select("select friends from friends where names ='"+name+"';");
if(favoriteResultSetChild.getCount()>0)
{while(favoriteResultSetChild.moveToNext()){
String childName =favoriteResultSetChild.getString(favoriteResultSetChild.getColumnIndex("friends"));
Log.e("google_child",""+childName);
GroupItemEntity gi = ge.new GroupItemEntity();
gi.Name = childName;
ge.GroupItemCollection.add(gi);
}}
favoriteResultSetChild.close();
mGroupCollection.add(ge);
}}
favoriteResultSet.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
DBSQLite.closeDatabase();
}
}
package shenhengbin.practice;
导入java.util.ArrayList;
导入java.util.List;
导入shenhengbin.practice.entity.Constants;
导入shenhengbin.practice.entity.DBSQLite;
导入shenhengbin.practice.entity.GroupEntity;
导入shenhengbin.practice.entity.GroupEntity.GroupItemEntity;
导入android.app.Activity;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemLongClickListener;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ExpandableListView;
导入android.widget.SimpleCursorTreeAdapter;
导入android.widget.ExpandableListView.OnChildClickListener;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
私有可扩展列表视图mExpandableListView;
私有列表视图列表视图;
私人名单收集;
专用BaseExpandableListAdapter适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
设置默认值();
setupEvents();
}
私有void init(){
listView=(listView)findViewById(R.id.listView);
mExpandableListView=(ExpandableListView)findViewById(R.id.ExpandableListView);
}
私有void setupDefaults(){
prepareResource();
}
私有void setupEvents(){
adapter=新的ExpandableListAdapter(this,mExpandableListView,mGroupCollection);
setAdapter(适配器);
setOnChildClickListener(新的OnChildClickListener()){
@凌驾
公共布尔onChildClick(ExpandableListView父视图,视图v,
int groupPosition、int childPosition、long id){
Log.e(“儿童”,“点击”);
返回false;
}
});
mExpandableListView.setOnItemLongClickListener(新的OnItemLongClickListener(){
@凌驾
长单击(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
Log.e(“长”、“点击启用”);
返回false;
}
});
}
私有void prepareResource(){
mGroupCollection=新的ArrayList();
openDatabase(getApplicationContext());
游标favoriteResultSet=Constants.SQLITE_CONNECTION.select(“从名称中选择*);
如果(favoriteResultSet.getCount()>0)
{while(favoriteResultSet.moveToNext()){
字符串名称=favoriteResultSet.getString(favoriteResultSet.getColumnIndex(“名称”);
Log.e(“google_u3;”,即“+name”);
GroupEntity ge=新的GroupEntity();
ge.Name=名称;
游标favoriteResultSetChild=Constants.SQLITE_CONNECTION.select(“从朋友中选择朋友,其中名称=”“+name+”;”);
if(favoriteResultSetChild.getCount()>0)
{while(favoriteResultSetChild.moveToNext()){
String childName=favoriteResultSetChild.getString(favoriteResultSetChild.getColumnIndex(“朋友”);
Log.e(“google_child”,即“+childName”);
GroupItemEntity gi=ge.new GroupItemEntity();
gi.Name=childName;
ge.GroupItemCollection.add(gi);
}}
favoriteResultSetChild.close();
mGroupCollection.add(通用电气);
}}
favoriteResultSet.close();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
DBSQLite.closeDatabase();
}
}
ExpandableListAdapter.java
package shenhengbin.practice;
import java.util.List;
import shenhengbin.practice.entity.GroupEntity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;
public ExpandableListAdapter(Context pContext,ExpandableListView pExpandableListView,List<GroupEntity> pGroupCollection) {
mContext = pContext;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
}
private void setListEvent() {
mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int arg0) {
groupStatus[arg0] = 1;
}});
mExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int arg0) {
groupStatus[arg0] = 0;
}});
}
@Override
public String getChild(int arg0, int arg1) {
return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return super.getChildType(groupPosition, childPosition);
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return super.getCombinedChildId(groupId, childId);
}
@Override
public long getCombinedGroupId(long groupId) {
return super.getCombinedGroupId(groupId);
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
ChildHolder childHolder;
if (arg3 == null) {
arg3 = LayoutInflater.from(mContext).inflate(R.layout.list_group_item, null);
childHolder = new ChildHolder();
childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
arg3.setTag(childHolder);
}
else {childHolder = (ChildHolder) arg3.getTag();}
childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name);
return arg3;
}
@Override
public int getChildrenCount(int arg0) {
return mGroupCollection.get(arg0).GroupItemCollection.size();
}
@Override
public Object getGroup(int arg0) {
return mGroupCollection.get(arg0);
}
@Override
public int getGroupCount() {
return mGroupCollection.size();
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
GroupHolder groupHolder;
if (arg2 == null) {
arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,null);
groupHolder = new GroupHolder();
groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
arg2.setTag(groupHolder);
}
else {
groupHolder = (GroupHolder) arg2.getTag();
}
if (groupStatus[arg0] == 0) {
groupHolder.img.setImageResource(R.drawable.group_down);
}
else
{
groupHolder.img.setImageResource(R.drawable.group_up);
}
groupHolder.title.setText(mGroupCollection.get(arg0).Name);
return arg2;
}
class GroupHolder {
ImageView img;
TextView title;
}
class ChildHolder {
TextView title;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
package shenhengbin.practice;
导入java.util.List;
导入shenhengbin.practice.entity.GroupEntity;
导入android.content.Context;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.ExpandableListView;
导入android.widget.ExpandableListView.OnGroupClickListener;
导入android.widget.ExpandableListView.OnGroupCollapseListener;
导入android.widget.ExpandableListView.OnGroupExpandListener;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私有可扩展列表视图mExpandableListView;
私人名单收集;
私有int[]组状态;
公共ExpandableListAdapter(上下文pContext、ExpandableListView、pExpandableListView、列表pGroupCollection){
mContext=pContext;
mGroupCollection=pGroupCollection;
mExpandableListView=pExpandableListView;
groupStatus=newint[mGroupCollection.size()];
setListEvent();
}
私有void setListEvent(){
mExpandableListView.setOnGroupExpandListener(新的OnGroupExpandListener()){
@凌驾
public void onGroupExpand(int arg0){
groupStatus[arg0]=1;
}});
mExpandableListView.setOnGroupCollapseListener(新OnGroupCollapseListener(){
@凌驾
公共无效组折叠(int arg0){
groupStatus[arg0]=0;
}});
}
@凌驾
公共字符串getChild(int arg0,int arg1){
返回mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回0;
}
@凌驾
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}