我正在使用ExpandableListView來創建帶有子項目的自定義列表。我的第一個列表已成功創建,但不可點擊,因此無法展開。ExpandableListView在點擊時不會展開
這裏是我的代碼...
private ExpandableListView listview;
private TheaterListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.theater_list);
this.m_movieList = "A list of Movie (Custom Object)"
listview = (ExpandableListView) findViewById(R.id.theater_listview);
adapter = new TheaterListAdapter(getApplicationContext());
listview.setAdapter(adapter);
private class TheaterListAdapter extends BaseExpandableListAdapter {
public TheaterListAdapter(Context c) {
mContext = c;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
String MovieId = m_movieList.getMovies().get(groupPosition)
.getShowTimes().get(childPosition).getMovieId();
for (MovieInfo movie : m_movieList.getMovies()) {
if (MovieId.equalsIgnoreCase(movie.getId())) {
return movie;
}
}
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ExpandedHolder holder;
View rowView = convertView;
if (rowView == null) {
holder = new ExpandedHolder();
LayoutInflater li = getLayoutInflater();
rowView = li.inflate(R.layout.theater_expanded_view, null);
holder.img_movie_poster = (ImageView) rowView
.findViewById(R.id.imgMoviePoster);
holder.img_rating_star = (ImageView) rowView
.findViewById(R.id.imgRatingStar);
holder.txt_movie_name = (TextView) rowView
.findViewById(R.id.txtMovieName);
holder.txt_pg_duration = (TextView) rowView
.findViewById(R.id.txtPgDuration);
holder.txt_showtimes = (TextView) rowView
.findViewById(R.id.txtShowTimes);
rowView.setTag(holder);
} else {
holder = (ExpandedHolder) rowView.getTag();
}
holder.txt_movie_name.setText("Twilight");
holder.txt_pg_duration.setText("PG 90 min");
holder.txt_showtimes.setText("7:00 8:00");
return rowView;
}
@Override
public int getChildrenCount(int groupPosition) {
String thid = m_movieList.getThreaters().get(groupPosition).getId();
int cnt = 5;
for (ShowTimes st : m_movieList.getMovies().get(groupPosition)
.getShowTimes()) {
if (thid.equalsIgnoreCase(st.getTheaterId())) {
cnt = cnt + 1;
}
}
return cnt;
}
@Override
public Object getGroup(int groupPosition) {
return m_movieList.getThreaters().get(groupPosition);
}
@Override
public int getGroupCount() {
return m_movieList.getThreaters().size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final ParentHolder holder;
View rowView = convertView;
try {
if (rowView == null) {
holder = new ParentHolder();
LayoutInflater li = getLayoutInflater();
rowView = li.inflate(R.layout.theater_list_item, null);
holder.img_star = (ImageView) rowView
.findViewById(R.id.imgTheaterStar);
holder.txt_title = (TextView) rowView
.findViewById(R.id.txtTheaterTitle);
holder.txt_address1 = (TextView) rowView
.findViewById(R.id.txtTheaterAddress1);
holder.txt_address2 = (TextView) rowView
.findViewById(R.id.txtTheaterAddress2);
holder.txt_distance = (TextView) rowView
.findViewById(R.id.txtTheaterDistance);
holder.btn_map = (Button) rowView
.findViewById(R.id.btnMapIcon);
rowView.setTag(holder);
} else {
holder = (ParentHolder) rowView.getTag();
}
holder.txt_title
.setText(((TheaterInfo)getGroup(groupPosition)).getName());
holder.txt_address1.setText("3003 North Thanksgiving way");
holder.txt_address2.setText("Lehi, UT, United States");
holder.txt_distance.setText("15.0 mi");
} catch (Exception e) {
}
return rowView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
/**
* Create Viewholder to use for inflating imageviews and textview
*/
static class ParentHolder {
Button btn_map;
ImageView img_star;
TextView txt_title;
TextView txt_address1;
TextView txt_address2;
TextView txt_distance;
}
/**
* Create Viewholder to use for inflating imageviews and textview
*/
static class ExpandedHolder {
ImageView img_movie_poster;
ImageView img_rating_star;
TextView txt_movie_name;
TextView txt_pg_duration;
TextView txt_showtimes;
}
}