Back to project page Weather.
The source code is released under:
GNU General Public License
If you think the Android project Weather listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.imlongluo.weather.location; /* www . ja v a 2 s. com*/ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; /** * * @author longluo * ?????????????????<br /> * ?????????????A,B????????????group1?? * <code> * String[] groups={"group1", "group2", "group3"}; * String[][] childs={{"A", "B"},{"C", "D"},{"E", "F"}}; * provinceList= (ExpandableListView)findViewById(R.id.provinceList); * BaseExpandableListAdapter adapter=new MyListAdapter(this, groups, childs); * provinceList.setAdapter(adapter); * </code> * ?????????????????? */ public class MyListAdapter extends BaseExpandableListAdapter implements Filterable { /** * ?????????????? */ private String[] groups; /** * ?????????????? */ private String[][] childs; /** * ?????????????? */ private String[] allGroups; /** * ?????????????? */ private String[][] allChilds; /** * ?????Context???????TextView?? * @see #getGenericView */ private Context context; /** * ????????? */ private CityFilter filter; /** * ?????????????????? */ private ExpandableListView provinceList; /** * ??????????????????????????????????????????????? * ???childs???groups?????? * <br /> * @param context ? <code>Context</code>????????????????? * @param groups ?<code>String</code>??????????????????? * @param childs ?<code>String</code>????,?groups???????????????????? */ public MyListAdapter(Context context, ExpandableListView listView, String[] groups, String[][] childs) { this.context = context; this.groups = groups; this.childs = childs; allGroups = groups; allChilds = childs; this.provinceList = listView; } /** * ?????????????<code>groupPosition</code>?????????????<code>childPosition</code> * ???????????<br /> * @param groupPostion????????????? * @param childPostion?????????????? * @return ???????????? */ @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; } /** * ?????????????<code>groupPosition</code>?????????????<code>childPosition</code> * ?????????ID * @param groupPostion????????????? * @param childPostion?????????????? * @return ????????????ID */ @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } /** * @return ????????<code>View<code>?? */ @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = null; //????convertView?????????????????????? if(convertView==null) { //????TextView?? textView = getGenericView(); //???????? textView.setText(getChild(groupPosition, childPosition).toString()); } else { textView = (TextView)convertView; textView.setText(getChild(groupPosition, childPosition).toString()); } return textView; } /** * ???????????????? * @param groupPosition???????? * @return ????????? */ @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } /** * ????????<code>groupPosition</code>??????????? * @return ??????? */ @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } /** * ??????? * @return ???????? */ @Override public int getGroupCount() { return groups.length; } /** * ??????ID,?????? * @param groupPosition????????? * @return ????ID??<code>0</code>?? */ @Override public long getGroupId(int groupPosition) { return groupPosition; } /** * @return ???????<code>View</code>???? */ @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = null; if(convertView==null){ textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); }else { textView = (TextView)convertView; textView.setText(getGroup(groupPosition).toString()); } return textView; } @Override public boolean hasStableIds() { return true; } /** * ????????????????? * @param groupPosition ???????? * @param childPosition ???????? * @return ??? */ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } /** * ????TextView???? * @return ??TextView???? */ private TextView getGenericView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 48); TextView textView = new TextView(context); textView.setLayoutParams(lp); textView.setTextSize(18); textView.setTextColor(Color.WHITE); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(32, 0, 0, 0); return textView; } /** * ???????????? * @return Filter ??????? */ @Override public Filter getFilter() { if(filter == null) { filter = new CityFilter(); } return filter; } private class CityFilter extends Filter { /** * ???????????????????????????????FilterResults * @param constraint ?????????? * @return ??????????? */ @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); //???????????????????,????????????? Map<Integer, ArrayList<Integer>> values = new HashMap<Integer, ArrayList<Integer>>(); //?????????????????????? if(constraint == null || constraint.length() == 0) { for(int i=0; i<allGroups.length; i++) { ArrayList<Integer> index = new ArrayList<Integer>(); //??????????? for(int j=0; j<allChilds[i].length; j++) { index.add(j); } values.put(i, index); } } else { String filterStr = constraint.toString(); for(int i=0; i<allGroups.length; i++) { //??????????????????????? if(allGroups[i].contains(filterStr)) { ArrayList<Integer> index = new ArrayList<Integer>(); //??????????? for(int j=0; j<allChilds[i].length; j++) { index.add(j); } values.put(i, index); } else { ArrayList<Integer> index = new ArrayList<Integer>(); //????????????????????????????????? for(int j=0; j<allChilds[i].length; j++) { if(allChilds[i][j].contains(filterStr)) { index.add(j); } } //?????????????????????????? if(index.size() > 0) { values.put(i, index); } else { index = null; } } } } results.values = values; results.count = values.size(); return results; } /** * ??????????results?????????????? * @param constraint ?????? * @param results ??????? */ @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { //??????????????? Map<Integer, ArrayList<Integer>> filterResult = (Map<Integer, ArrayList<Integer>>) results.values; int count = filterResult.size(); //????? if( count > 0) { String[] newGroups = new String[count]; String[][] newChilds = new String[count][]; int index = 0; int length = 0; //????groups?childs for(int i=0; i<allGroups.length; i++) { if(filterResult.containsKey(i)) { newGroups[index] = allGroups[i]; //??????????? ArrayList<Integer> citys = filterResult.get(i); length = citys.size(); newChilds[index] = new String[length]; for(int j = 0; j< length; j++) { newChilds[index][j] = allChilds[i][citys.get(j)]; } index = index + 1; } } //??groups?childs groups = newGroups; childs = newChilds; //???? notifyDataSetChanged(); //?????????? count = getGroupCount(); if(count < 34) { //??????? for(int i=0; i<count; i++) { provinceList.expandGroup(i); } } else { //??????? for(int i=0; i<count; i++) { provinceList.collapseGroup(i); } } } else { //??????????????????? notifyDataSetInvalidated(); } } } }