Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 chris 1
package com.gebauz.ZhuyinQuiz;
2
 
3
import android.app.Activity;
4
import android.content.Context;
5
import android.graphics.Color;
6
import android.os.Bundle;
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 ZhuyinTableActivity extends Activity
15
{
16
        public class TableAdapter extends BaseAdapter
17
        {
18
            private Context mContext;
19
            private ZhuyinTable mZhuyinTable;
20
 
21
            public TableAdapter(Context c)
22
            {
23
                mContext = c;
24
                mZhuyinTable = new ZhuyinTable(Constants.QUIZ_ALL);
25
            }      
26
 
27
            public int getCount()
28
            {
29
                return mZhuyinTable.getCount();
30
            }
31
 
32
            public Object getItem(int position)
33
            {
34
                return null;
35
            }
36
 
37
            public long getItemId(int position)
38
            {
39
                return 0;
40
            }
41
 
42
            // create a new ImageView for each item referenced by the Adapter
43
            public View getView(int position, View convertView, ViewGroup parent)
44
            {
45
                TextView view;
46
                if (convertView == null)
47
                {
48
                        // if it's not recycled, initialize some attributes
49
                        view = new TextView(mContext);
50
 
51
                        final float scale = mContext.getResources().getDisplayMetrics().density;
52
                        int buttonSize = (int) (Constants.TABLE_ITEM_SIZE_DIP * scale + 0.5f);
53
 
54
                        view.setLayoutParams(new GridView.LayoutParams(buttonSize, buttonSize));
55
                        view.setPadding(8, 8, 8, 8);
56
                        view.setTextColor(Color.WHITE);
57
                        view.setBackgroundColor(Constants.GRID_BACKGROUND_COLOR);
58
                        view.setGravity(Gravity.CENTER);
59
                }
60
                else
61
                {
62
                        view = (TextView)convertView;
63
                }
64
 
65
                String textItem = mZhuyinTable.getZhuyin(position) + "\n" + mZhuyinTable.getPinyin(position);
66
                if (mZhuyinTable.getExtraPronounciation(position).length() > 0)
67
                        textItem = textItem + " " + mZhuyinTable.getExtraPronounciation(position);
68
 
69
                // make text size smaller for longer texts
70
                if ((mZhuyinTable.getPinyin(position).length() + mZhuyinTable.getExtraPronounciation(position).length()) > 4)
71
                        view.setTextSize(Constants.TABLE_TEXT_SIZE_LONG_ITEM);
72
                else
73
                        view.setTextSize(Constants.TABLE_TEXT_SIZE_SHORT_ITEM);
74
 
75
                view.setText(textItem);
76
 
77
                return view;
78
            }
79
        }
80
 
81
    @Override
82
    public void onCreate(Bundle savedInstanceState)
83
    {
84
        super.onCreate(savedInstanceState);
85
        setContentView(R.layout.zhuyintable);
86
 
87
        GridView gridview = (GridView)findViewById(R.id.gridview);
88
        gridview.setAdapter(new TableAdapter(this));
89
    }
90
}
91