package com.example.wordbook; import java.util.List; import java.util.Map; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import com.example.wordbook.common.Common; /** * メイン画面用アダプタ */ public class MainAdapter extends SimpleAdapter { /** LayoutInflater */ private LayoutInflater mInflater; /** * ViewHolder */ private static class ViewHolder { TextView text1; TextView text2; ImageView bar_zero; ImageView bar_ok; ImageView bar_ng; ImageView handle; } /** * コンストラクタ * * @param context * コンテキスト * @param data * アイテム * @param resource * リソースID * @param from * マップキー * @param to * ウィジェットID */ public MainAdapter(Context context, List> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mInflater = LayoutInflater.from(context); } /* * (非 Javadoc) * * @see android.widget.Adapter#getView(int, android.view.View, * android.view.ViewGroup) */ @Override public View getView(final int position, View convertView, final ViewGroup parent) { // View設定 ViewHolder holder; View view = convertView; if (view == null) { view = mInflater.inflate(R.layout.list_main, parent, false); holder = new ViewHolder(); holder.text1 = (TextView) view.findViewById(android.R.id.text1); holder.text2 = (TextView) view.findViewById(android.R.id.text2); holder.bar_zero = (ImageView) view.findViewById(R.id.bar_zero); holder.bar_ok = (ImageView) view.findViewById(R.id.bar_ok); holder.bar_ng = (ImageView) view.findViewById(R.id.bar_ng); holder.handle = (ImageView) view.findViewById(R.id.handle); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // アイテム取得 @SuppressWarnings("unchecked") Map data = (Map) getItem(position); // 文字列設定 holder.text1.setText(data.get(Common.WORDBOOK_TITLE).toString()); holder.text2.setText(data.get(Common.WORDBOOK_VALUE).toString()); // グラフ設定 int count = (Integer) data.get(Common.WORDBOOK_COUNT); int zero = (Integer) data.get(Common.WORDBOOK_ZERO); int ok = (Integer) data.get(Common.WORDBOOK_OK); int ng = (Integer) data.get(Common.WORDBOOK_NG); double b_zero = (count == 0 ? 0.0 : zero * 100.0 / count); double b_ok = (ok + ng == 0 ? 0.0 : ok * 100.0 / (ok + ng)) * (1.0 - b_zero / 100.0); // 解答済項目数に対する割合 double b_ng = (ok + ng == 0 ? 0.0 : ng * 100.0 / (ok + ng)) * (1.0 - b_zero / 100.0); // 解答済項目数に対する割合 holder.bar_zero.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, (int) b_zero)); holder.bar_ok.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, (int) b_ok)); holder.bar_ng.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, (int) b_ng)); // アダプタからタッチイベントを送信できないためIDで識別 // タッチ用イベントリスナ設定 holder.handle.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { ((ListView) parent).performItemClick(v, position, Common.ITEM_TOUCH); } // ACTION_UPに対応するためイベントは消費しない return false; } }); // ドラッグ中アイテムハイライト処理 Boolean hl = (Boolean) data.get(Common.WORDBOOK_HL); if (hl != null && hl) { view.setBackgroundResource(R.drawable.back_ud); } else { view.setBackgroundColor(Color.TRANSPARENT); } return view; } }