嗨朋友们,我正在使用可扩展的列表视图...
我从数据库中获取值,它显示得很好,但是没有听众在下面工作是我的来源......
MainActivity.java
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();
}
}
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;
}
}
帮我。如何在可展开列表视图中启用所有点击侦听器。提前致谢。