Rev 25 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 25 | chris | 1 | package com.gebauz.KanaQuiz; |
| 2 | |||
| 3 | import java.util.Random; |
||
| 4 | |||
| 5 | import android.content.Context; |
||
| 6 | import android.graphics.Color; |
||
| 7 | import android.view.Gravity; |
||
| 8 | import android.view.View; |
||
| 9 | import android.view.ViewGroup; |
||
| 10 | import android.widget.BaseAdapter; |
||
| 11 | import android.widget.GridView; |
||
| 12 | import android.widget.TextView; |
||
| 13 | |||
| 14 | public class GridAdapter extends BaseAdapter |
||
| 15 | { |
||
| 16 | private Context mContext; |
||
| 17 | private KanaTable mKanaTable; |
||
| 18 | private int mQuizMode = -1; |
||
| 19 | private int mAnswerCharacters = -1; |
||
| 20 | |||
| 21 | private Random mRandomizer = new Random(); |
||
| 22 | private int[] mJumbledIds; |
||
| 23 | |||
| 24 | public GridAdapter(Context c, KanaTable table, int quizMode, int answerCharacters) |
||
| 25 | { |
||
| 26 | mContext = c; |
||
| 27 | mKanaTable = table; |
||
| 28 | mQuizMode = quizMode; |
||
| 29 | mAnswerCharacters = answerCharacters; |
||
| 30 | |||
| 31 | mJumbledIds = new int[mKanaTable.getCount()]; |
||
| 32 | |||
| 33 | jumbleButtons(false); |
||
| 34 | } |
||
| 35 | |||
| 36 | public void jumbleButtons(boolean doJumble) |
||
| 37 | { |
||
| 38 | for (int i = 0; i < mJumbledIds.length; i++) |
||
| 39 | { |
||
| 40 | mJumbledIds[i] = i; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (doJumble) |
||
| 44 | { |
||
| 45 | // Fisher-Yates shuffle algorithm |
||
| 46 | for (int i = (mJumbledIds.length-1); i > 0; i--) |
||
| 47 | { |
||
| 48 | int j = mRandomizer.nextInt(i); |
||
| 49 | int temp = mJumbledIds[j]; |
||
| 50 | mJumbledIds[j] = mJumbledIds[i]; |
||
| 51 | mJumbledIds[i] = temp; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public int getActualItem(int position) |
||
| 57 | { |
||
| 58 | return mJumbledIds[position]; |
||
| 59 | } |
||
| 60 | |||
| 61 | public String getActualExtraPronounciation(int position) |
||
| 62 | { |
||
| 63 | return mKanaTable.getExtraPronounciation(getActualItem(position)); |
||
| 64 | } |
||
| 65 | |||
| 66 | public String getActualKana(int position) |
||
| 67 | { |
||
| 68 | return mKanaTable.getKana(getActualItem(position)); |
||
| 69 | } |
||
| 70 | |||
| 71 | public String getActualRomaji(int position) |
||
| 72 | { |
||
| 73 | return mKanaTable.getRomaji(getActualItem(position)); |
||
| 74 | } |
||
| 75 | |||
| 76 | public int nextRandomItem() |
||
| 77 | { |
||
| 78 | if (mAnswerCharacters == Constants.INPUT_SMALL) |
||
| 79 | return mRandomizer.nextInt(Constants.INPUT_SMALL_COUNT); |
||
| 80 | return mRandomizer.nextInt(mKanaTable.getCount()); |
||
| 81 | } |
||
| 82 | |||
| 83 | public int getCount() |
||
| 84 | { |
||
| 85 | if (mAnswerCharacters == Constants.INPUT_SMALL) |
||
| 86 | return Constants.INPUT_SMALL_COUNT; |
||
| 87 | |||
| 88 | return mKanaTable.getCount(); |
||
| 89 | } |
||
| 90 | |||
| 91 | public Object getItem(int position) |
||
| 92 | { |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | |||
| 96 | public long getItemId(int position) |
||
| 97 | { |
||
| 98 | return 0; |
||
| 99 | } |
||
| 100 | |||
| 101 | // create a new ImageView for each item referenced by the Adapter |
||
| 102 | public View getView(int position, View convertView, ViewGroup parent) |
||
| 103 | { |
||
| 104 | TextView button; |
||
| 105 | if (convertView == null) |
||
| 106 | { |
||
| 107 | // if it's not recycled, initialize some attributes |
||
| 108 | button = new TextView(mContext); |
||
| 109 | |||
| 110 | final float scale = mContext.getResources().getDisplayMetrics().density; |
||
| 111 | int buttonSize = (int) (Constants.GRID_BUTTON_SIZE_DIP * scale + 0.5f); |
||
| 112 | |||
| 113 | button.setLayoutParams(new GridView.LayoutParams(buttonSize, buttonSize)); |
||
| 114 | button.setPadding(8, 8, 8, 8); |
||
| 115 | button.setTextColor(Color.WHITE); |
||
| 116 | button.setBackgroundColor(Constants.GRID_BACKGROUND_COLOR); |
||
| 117 | button.setGravity(Gravity.CENTER); |
||
| 118 | } |
||
| 119 | else |
||
| 120 | { |
||
| 121 | button = (TextView)convertView; |
||
| 122 | } |
||
| 123 | |||
| 124 | int actualItem = getActualItem(position); |
||
| 125 | |||
| 126 | String textItem = "-"; |
||
| 127 | switch (mQuizMode) |
||
| 128 | { |
||
| 129 | case Constants.MODE_ROMAJI: |
||
| 130 | // need zhuyin table in pinyin quiz mode |
||
| 131 | textItem = mKanaTable.getKana(actualItem); |
||
| 132 | break; |
||
| 133 | case Constants.MODE_KANA: |
||
| 134 | // need pinyin table in zhuyin quiz mode |
||
| 135 | textItem = mKanaTable.getRomaji(actualItem); |
||
| 136 | if (mKanaTable.getExtraPronounciation(actualItem).length() > 0) |
||
| 137 | textItem = textItem + "\n" + mKanaTable.getExtraPronounciation(actualItem); |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | |||
| 141 | // make text size smaller for longer texts |
||
| 34 | chris | 142 | if (textItem.length() > 4) |
| 25 | chris | 143 | button.setTextSize(Constants.GRID_TEXT_SIZE_LONG_ITEM); |
| 144 | else |
||
| 145 | button.setTextSize(Constants.GRID_TEXT_SIZE_SHORT_ITEM); |
||
| 146 | |||
| 147 | button.setText(textItem); |
||
| 148 | return button; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 |