public ListAdapter(Context context) { this.inflater = LayoutInflater.from(context); alphaIndexer = new HashMap<String, Integer>(); sections = new String[ShowCity_lists.size()]; for (int i = 0; i < ShowCity_lists.size(); i++) { // 当前汉语拼音首字母 String currentStr = getAlpha(ShowCity_lists.get(i).getPinyi()); // 上一个汉语拼音首字母,如果不存在为“ ” String previewStr = (i - 1) >= 0 ? getAlpha(ShowCity_lists.get(i - 1).getPinyi()) : " "; if (!previewStr.equals(currentStr)) { String name = getAlpha(ShowCity_lists.get(i).getPinyi()); alphaIndexer.put(name, i); sections[i] = name; } } }
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; int viewType = getItemViewType(position); if (convertView == null) { convertView = inflater.inflate(R.layout.hotel_list_item, null); holder = new ViewHolder(); holder.alpha = (TextView) convertView.findViewById(R.id.alpha); holder.name = (TextView) convertView.findViewById(R.id.name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // if (sh.getText().length()==0) {//搜所状态 // holder.name.setText(list.get(position).getName()); // holder.alpha.setVisibility(View.GONE); // }else if(position>0){ // 显示拼音和热门城市,一次检查本次拼音和上一个字的拼音,如果一样则不显示,如果不一样则显示 holder.name.setText(ShowCity_lists.get(position).getName()); String currentStr = getAlpha(ShowCity_lists.get(position).getPinyi()); // 本次拼音 String previewStr = (position - 1) >= 0 ? getAlpha(ShowCity_lists.get(position - 1).getPinyi()) : " "; // 上一个拼音 if (!previewStr.equals(currentStr)) { // 不一样则显示 holder.alpha.setVisibility(View.VISIBLE); if (currentStr.equals("#")) { currentStr = "热门城市"; } holder.alpha.setText(currentStr); } else { holder.alpha.setVisibility(View.GONE); } // } return convertView; }
@Override public Object getItem(int position) { return ShowCity_lists.get(position); }
@Override public int getCount() { return ShowCity_lists.size(); }