ZoneItemAdapter.java 6.96 KB
package com.sw.laryngoscope.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.icu.text.TimeZoneNames;
import android.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sw.laryngoscope.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import static com.sw.laryngoscope.common.InitParam.ZONE_COUNTRYNAME;
import static com.sw.laryngoscope.common.InitParam.ZONE_ID;

public class ZoneItemAdapter extends BaseAdapter {

	private String TAG = getClass().getSimpleName();

	private LayoutInflater mInflater;

	private OnClickListener mlisten;

	private List<HashMap<String, Object>> beans;

	// 定义Context
	private Context mContext;

	public ZoneItemAdapter(Context c, List<HashMap<String, Object>> paramArrayList) {
		mContext = c.getApplicationContext();
		mInflater = LayoutInflater.from(c);
		this.beans = paramArrayList;
	}

	public void setBeans(List<HashMap<String, Object>> beans) {
		this.beans = beans;
	}

	/*public void setItemStatus(int item[]) {
		beans = item;
	}*/

	/*public void setIsUpdate(int id, boolean param) {
		isUpdate[id] = param;
	}*/

	public int getCount() {
		/*Log.d(TAG, "==================" + mImageIds.length);*/
		return beans.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}
	
	public void setUpdateListener(OnClickListener listen) {
		mlisten = listen;
	}
	
	@SuppressLint("ResourceAsColor") 
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null; 
		if ( convertView == null ) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_lan_choose, null);
			//设置宽度和高度
			ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, 68);
			convertView.setLayoutParams(params);
			holder.layout_lan_item = (LinearLayout)convertView.findViewById(R.id.layout_lan_item);
			holder.item_lan = (TextView)convertView.findViewById(R.id.item_lan);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}

		HashMap<String, Object> bean = this.beans.get(position);
		//holder.item_lan.setText( bean.get(ZONE_COUNTRYNAME) + "");

		final Locale locale = Locale.getDefault();
		final Date now = new Date();
		TimeZone tz = TimeZone.getTimeZone((String)bean.get(ZONE_ID));
		String displayName = getZoneDisplayName(locale, tz, now, false);
		holder.item_lan.setText( displayName + "");

		//holder.item_lan.setText( TimeZone.getTimeZone( (String)bean.get(ZONE_ID) ).getDisplayName() + "");
		if ( !TimeZone.getDefault().getID().equals(
				TimeZone.getTimeZone( (String)bean.get(ZONE_ID) ).getID() ) ) {
			holder.layout_lan_item.setBackgroundResource(R.drawable.bg_line_back_0);
			/*holder.lay_userlist.setBackgroundColor(
					mContext.getResources().getColor(R.color.color_white));*/
			holder.item_lan.setTextColor(
					mContext.getResources().getColor(R.color.color_white));
			holder.item_lan.setTextSize(mContext.getResources().getDimension(R.dimen.text_size_24));
		} else {
			holder.layout_lan_item.setBackgroundResource(R.drawable.bg_line_back_1);
			holder.item_lan.setTextColor(
					mContext.getResources().getColor(R.color.color_0aede0));
			holder.item_lan.setTextSize(mContext.getResources().getDimension(R.dimen.text_size_28));
		}

		return convertView;
	}

	public final class ViewHolder {
		public LinearLayout layout_lan_item;
		public TextView item_lan;
	}
	
	public interface OnClickListener {
        void onClickEvent(int id);
    }

	private String getZoneDisplayName(Locale locale, TimeZone tz, Date now,
									  boolean preferLongName) {
		String zoneNameString;
		if (preferLongName) {
			zoneNameString = getZoneLongName(locale, tz, now);
		} else {
			zoneNameString = getZoneExemplarLocation(locale, tz);
			if (zoneNameString == null || zoneNameString.isEmpty()) {
				// getZoneExemplarLocation can return null.
				zoneNameString = getZoneLongName(locale, tz, now);
			}
		}
		return zoneNameString;
	}

	private String getZoneExemplarLocation(Locale locale, TimeZone tz) {
		final TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(locale);
		return timeZoneNames.getExemplarLocationName(/*locale.toString(), */tz.getID());
	}

	private String getZoneLongName(Locale locale, TimeZone tz, Date now) {
		boolean daylight = tz.inDaylightTime(now);
		// This returns a name if it can, or will fall back to GMT+0X:00 format.
		return tz.getDisplayName(daylight, TimeZone.LONG, locale);
	}

	/*public static String getTimeZoneOffsetAndName(TimeZone tz, Date now) {
		Locale locale = Locale.getDefault();
		String gmtString = getGmtOffsetString(locale, tz, now);
		String zoneNameString = getZoneLongName(locale, tz, now);
		if (zoneNameString == null) {
			return gmtString;
		}

		// We don't use punctuation here to avoid having to worry about localizing that too!
		return gmtString + " " + zoneNameString;
	}
	private static String getZoneLongName(Locale locale, TimeZone tz, Date now) {
		boolean daylight = tz.inDaylightTime(now);
		// This returns a name if it can, or will fall back to GMT+0X:00 format.
		return tz.getDisplayName(daylight, TimeZone.LONG, locale);
	}
	private static String getGmtOffsetString(Locale locale, TimeZone tz, Date now) {
		// Use SimpleDateFormat to format the GMT+00:00 string.
		SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
		gmtFormatter.setTimeZone(tz);
		String gmtString = gmtFormatter.format(now);

		// Ensure that the "GMT+" stays with the "00:00" even if the digits are RTL.
		BidiFormatter bidiFormatter = BidiFormatter.getInstance();
		boolean isRtl = TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL;
		gmtString = bidiFormatter.unicodeWrap(gmtString,
				isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);
		return gmtString;
	}

	public static String getTimeZoneText(TimeZone tz, boolean includeName) {
		Date now = new Date();

		SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
		gmtFormatter.setTimeZone(tz);
		String gmtString = gmtFormatter.format(now);
		BidiFormatter bidiFormatter = BidiFormatter.getInstance();
		Locale l = Locale.getDefault();
		boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL;
		gmtString = bidiFormatter.unicodeWrap(gmtString,
				isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);

		if (!includeName) {
			return gmtString;
		}

		return gmtString;
	}*/
	
}