Subversion Repositories AndroidProjects

Rev

Rev 14 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.ZhuyinQuiz;

import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class GridAdapter extends BaseAdapter
{
    private Context mContext;
    private ZhuyinTable mZhuyinTable;
    private int mQuizMode = -1;
    private int mAnswerCharacters = -1;
       
    private Random mRandomizer = new Random();
    private int[] mJumbledIds;
   
    public GridAdapter(Context c, ZhuyinTable table, int quizMode, int answerCharacters)
    {
        mContext = c;
        mZhuyinTable = table;
        mQuizMode = quizMode;
        mAnswerCharacters = answerCharacters;
       
        mJumbledIds = new int[mZhuyinTable.getCount()];
       
        jumbleButtons(false);
    }
   
    public void jumbleButtons(boolean doJumble)
    {
        for (int i = 0; i < mJumbledIds.length; i++)
        {
                mJumbledIds[i] = i;
        }
       
        if (doJumble)
        {
                // Fisher-Yates shuffle algorithm
                        for (int i = (mJumbledIds.length-1); i > 0; i--)
                        {
                                int j = mRandomizer.nextInt(i);
                                int temp = mJumbledIds[j];
                                mJumbledIds[j] = mJumbledIds[i];
                                mJumbledIds[i] = temp;
                        }
        }
    }
    /*
    public void putCorrectAnswerInFront(int correctAnswer)
    {
        // in 8-characters mode, if correct answer not among first 8 entries, randomly set one to it
        if (mAnswerCharacters == Constants.INPUT_SMALL)
        {
                for (int i = 0; i < Constants.INPUT_SMALL_COUNT; i++)
                {
                        if (mJumbledIds[i] == mJumbledIds[correctAnswer])
                                return;
                }
               
                // not found, manually put one into first 8
                int where = mRandomizer.nextInt(8);
                int temp = mJumbledIds[where];
                mJumbledIds[where] = mJumbledIds[correctAnswer];
                mJumbledIds[correctAnswer] = temp;
        }
    }*/

   
    public int getActualItem(int position)
    {
        return mJumbledIds[position];
    }
   
    public String getActualExtraPronounciation(int position)
    {
        return mZhuyinTable.getExtraPronounciation(getActualItem(position));
    }
   
    public String getActualZhuyin(int position)
    {
        return mZhuyinTable.getZhuyin(getActualItem(position));
    }

    public String getActualPinyin(int position)
    {
        return mZhuyinTable.getPinyin(getActualItem(position));
    }
   
    public int nextRandomItem()
    {
        if (mAnswerCharacters == Constants.INPUT_SMALL)
                return mRandomizer.nextInt(Constants.INPUT_SMALL_COUNT);
        return mRandomizer.nextInt(mZhuyinTable.getCount());
    }
   
    public int getCount()
    {
        if (mAnswerCharacters == Constants.INPUT_SMALL)
                return Constants.INPUT_SMALL_COUNT;
       
        return mZhuyinTable.getCount();
    }

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

    public long getItemId(int position)
    {
        return 0;
    }
   
    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView button;
        if (convertView == null)
        {
                // if it's not recycled, initialize some attributes
                button = new TextView(mContext);
               
                final float scale = mContext.getResources().getDisplayMetrics().density;
                int buttonSize = (int) (Constants.GRID_BUTTON_SIZE_DIP * scale + 0.5f);
               
                button.setLayoutParams(new GridView.LayoutParams(buttonSize, buttonSize));
                button.setPadding(8, 8, 8, 8);
                button.setTextColor(Color.WHITE);
                button.setBackgroundColor(Constants.GRID_BACKGROUND_COLOR);
                button.setGravity(Gravity.CENTER);
        }
        else
        {
                button = (TextView)convertView;
        }
       
        int actualItem = getActualItem(position);
               
        String textItem = "-";
        switch (mQuizMode)
        {
        case Constants.MODE_PINYIN:
                // need zhuyin table in pinyin quiz mode
                textItem = mZhuyinTable.getZhuyin(actualItem);
                break;
        case Constants.MODE_ZHUYIN:
                // need pinyin table in zhuyin quiz mode
                textItem = mZhuyinTable.getPinyin(actualItem);
                if (mZhuyinTable.getExtraPronounciation(actualItem).length() > 0)
                        textItem = textItem + "\n" + mZhuyinTable.getExtraPronounciation(actualItem);
                break;
        }
       
        // make text size smaller for longer texts
        if (textItem.length() > 2)
                button.setTextSize(Constants.GRID_TEXT_SIZE_LONG_ITEM);
        else
                button.setTextSize(Constants.GRID_TEXT_SIZE_SHORT_ITEM);
       
        button.setText(textItem);
        return button;
    }
}