package com.example.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.example.entity.TVchannel;
import com.example.entity.TVstation;
import com.example.utils.Consts;
import com.example.view.R;
import com.example.view.TVProgramActivity;
public class TVChannelAdapter extends BaseExpandableListAdapter {
private ArrayList<Object> stations;
private ArrayList<ArrayList<Object>> tvchannels;
private LayoutInflater inflater;
private Context context;
public TVChannelAdapter(Context context, ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
this.context = context;
this.inflater = LayoutInflater.from(context);
setData(stations, tvchannels);
}
/**
* 更新数据
*
* @param stations
* @param tvchannels
*/
public void notifyDataChanged(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
setData(stations, tvchannels);
this.notifyDataSetChanged();
}
private void setData(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
if (stations == null) {
Log.i("info", "====station = " + stations);
this.stations = new ArrayList<Object>();
} else {
this.stations = stations;
}
if (tvchannels == null) {
// Log.i("info", "====tvchannels = "+tvchannels);
this.tvchannels = new ArrayList<ArrayList<Object>>();
} else {
this.tvchannels = tvchannels;
}
}
@Override
public int getGroupCount() {
Log.i("info", "==grouup Size = " + stations.size());
return stations.size();
}
/**
* 获取展开的子列个数:
*/
@Override
public int getChildrenCount(int groupPosition) {
Log.i("info", "===child size1 = " + tvchannels.size());
Log.i("info", "===child size2 = "
+ tvchannels.get(groupPosition).size());
return tvchannels.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return stations.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return tvchannels.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_main, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_main_stationname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
TVstation station = (TVstation) stations.get(groupPosition);
holder.tv_groupOrchild.setText(station.getTvName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_child, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_child_channelname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
final TVchannel tVchannel = (TVchannel) (tvchannels.get(groupPosition)
.get(childPosition));
holder.tv_groupOrchild.setText(tVchannel.getChnName());
//为每一项设置监听器
holder.tv_groupOrchild.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,TVProgramActivity.class);
intent.putExtra(Consts.FOR_PROGRAM_ACTIVITY, tVchannel);
context.startActivity(intent);
}
});
return convertView;
}
class viewHolder {
TextView tv_groupOrchild;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.example.entity.TVchannel;
import com.example.entity.TVstation;
import com.example.utils.Consts;
import com.example.view.R;
import com.example.view.TVProgramActivity;
public class TVChannelAdapter extends BaseExpandableListAdapter {
private ArrayList<Object> stations;
private ArrayList<ArrayList<Object>> tvchannels;
private LayoutInflater inflater;
private Context context;
public TVChannelAdapter(Context context, ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
this.context = context;
this.inflater = LayoutInflater.from(context);
setData(stations, tvchannels);
}
/**
* 更新数据
*
* @param stations
* @param tvchannels
*/
public void notifyDataChanged(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
setData(stations, tvchannels);
this.notifyDataSetChanged();
}
private void setData(ArrayList<Object> stations,
ArrayList<ArrayList<Object>> tvchannels) {
if (stations == null) {
Log.i("info", "====station = " + stations);
this.stations = new ArrayList<Object>();
} else {
this.stations = stations;
}
if (tvchannels == null) {
// Log.i("info", "====tvchannels = "+tvchannels);
this.tvchannels = new ArrayList<ArrayList<Object>>();
} else {
this.tvchannels = tvchannels;
}
}
@Override
public int getGroupCount() {
Log.i("info", "==grouup Size = " + stations.size());
return stations.size();
}
/**
* 获取展开的子列个数:
*/
@Override
public int getChildrenCount(int groupPosition) {
Log.i("info", "===child size1 = " + tvchannels.size());
Log.i("info", "===child size2 = "
+ tvchannels.get(groupPosition).size());
return tvchannels.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return stations.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return tvchannels.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_main, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_main_stationname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
TVstation station = (TVstation) stations.get(groupPosition);
holder.tv_groupOrchild.setText(station.getTvName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
viewHolder holder = null;
if (convertView == null) {
holder = new viewHolder();
convertView = inflater.inflate(R.layout.eplv_child, null);
holder.tv_groupOrchild = (TextView) convertView
.findViewById(R.id.tv_station_eplv_child_channelname);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
final TVchannel tVchannel = (TVchannel) (tvchannels.get(groupPosition)
.get(childPosition));
holder.tv_groupOrchild.setText(tVchannel.getChnName());
//为每一项设置监听器
holder.tv_groupOrchild.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,TVProgramActivity.class);
intent.putExtra(Consts.FOR_PROGRAM_ACTIVITY, tVchannel);
context.startActivity(intent);
}
});
return convertView;
}
class viewHolder {
TextView tv_groupOrchild;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}