Android动态改变GridView的值【安卓进化二十二】

来源:互联网 发布:怎么发送淘宝店铺链接 编辑:程序博客网 时间:2024/06/08 04:01
近感觉GridView的用法比较灵活,比如写一些动态改变的东西,更新显示效果比较明显,所以我写了一个demo,选中球后,点击确定显示选中的球在主页面,点击重置,则选中的球都被清空。这个效果还是比较不错的!希望给大家启迪。项目图片我没有上传,有问题的请留言,想要源码的请留言,转载请标明出处:

http://blog.csdn.net/wdaming1986/article/details/6786444

我的csdn资源下载链接分享给大家,大家可以下载:

http://download.csdn.net/detail/wdaming1986/3611738

 

                          程序的主页面:                                                      点击button按钮弹出dialog页面:

                                       

                                                                                              点击确定后的效果,点击重置如

                 选球,球的颜色会发生变化                                第二张图片的效果,球没有选中的效果

                                       

代码奉上:

com.cn.daming包里面的类:

一、MainActivity.java类中的代码:

[java] view plaincopyprint?
  1. package com.cn.daming;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.app.AlertDialog;  
  7. import android.graphics.Color;  
  8. import android.graphics.drawable.GradientDrawable;  
  9. import android.graphics.drawable.GradientDrawable.Orientation;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.GridView;  
  17. import android.widget.Toast;  
  18.   
  19. public class MainActivity extends Activity {  
  20.   
  21.     private GridView redBallGrid;  
  22.     private GridView blueBallGrid;  
  23.     private Button okButton;  
  24.     private Button resetButton;  
  25.       
  26.     private Button dialog_button;  
  27.     private GridView showRedGridView;  
  28.     private GridView showBlueGridView;  
  29.     private AlertDialog show_ball_dialog;  
  30.     private View show_ball_dialog_view;  
  31.       
  32.     public static ArrayList<Integer> redBallSelectArr= new ArrayList<Integer>();  
  33.     public static ArrayList<Integer> blueBallSelectArr= new ArrayList<Integer>();  
  34.       
  35.     private int redNumLeft = 35,blueNumLeft = 16;  
  36.       
  37.     private Integer[] changeimageid = {  
  38.             R.drawable.redball_dark01,  
  39.             R.drawable.redball_dark02,  
  40.             R.drawable.redball_dark03,  
  41.             R.drawable.redball_dark04,  
  42.             R.drawable.redball_dark05,  
  43.             R.drawable.redball_dark06,  
  44.             R.drawable.redball_dark07,  
  45.             R.drawable.redball_dark08,  
  46.             R.drawable.redball_dark09,  
  47.             R.drawable.redball_dark10,  
  48.             R.drawable.redball_dark11,  
  49.             R.drawable.redball_dark12,  
  50.             R.drawable.redball_dark13,  
  51.             R.drawable.redball_dark14,  
  52.             R.drawable.redball_dark15,  
  53.             R.drawable.redball_dark16,  
  54.             R.drawable.redball_dark17,  
  55.             R.drawable.redball_dark18,  
  56.             R.drawable.redball_dark19,  
  57.             R.drawable.redball_dark20,  
  58.             R.drawable.redball_dark21,  
  59.             R.drawable.redball_dark22,  
  60.             R.drawable.redball_dark23,  
  61.             R.drawable.redball_dark24,  
  62.             R.drawable.redball_dark25,  
  63.             R.drawable.redball_dark26,  
  64.             R.drawable.redball_dark27,  
  65.             R.drawable.redball_dark28,  
  66.             R.drawable.redball_dark29,  
  67.             R.drawable.redball_dark30,  
  68.             R.drawable.redball_dark31,  
  69.             R.drawable.redball_dark32,  
  70.             R.drawable.redball_dark33,  
  71.             R.drawable.redball_dark34,  
  72.             R.drawable.redball_dark35,  
  73.     };  
  74.       
  75.     private Integer[]   mImageIds   =   
  76.     {   
  77.             R.drawable.redball_01,  
  78.             R.drawable.redball_02,  
  79.             R.drawable.redball_03,  
  80.             R.drawable.redball_04,  
  81.             R.drawable.redball_05,  
  82.             R.drawable.redball_06,  
  83.             R.drawable.redball_07,  
  84.             R.drawable.redball_08,  
  85.             R.drawable.redball_09,  
  86.             R.drawable.redball_10,  
  87.             R.drawable.redball_11,  
  88.             R.drawable.redball_12,  
  89.             R.drawable.redball_13,  
  90.             R.drawable.redball_14,  
  91.             R.drawable.redball_15,  
  92.             R.drawable.redball_16,  
  93.             R.drawable.redball_17,  
  94.             R.drawable.redball_18,  
  95.             R.drawable.redball_19,  
  96.             R.drawable.redball_20,  
  97.             R.drawable.redball_21,  
  98.             R.drawable.redball_22,  
  99.             R.drawable.redball_23,  
  100.             R.drawable.redball_24,  
  101.             R.drawable.redball_25,  
  102.             R.drawable.redball_26,  
  103.             R.drawable.redball_27,  
  104.             R.drawable.redball_28,  
  105.             R.drawable.redball_29,  
  106.             R.drawable.redball_30,  
  107.             R.drawable.redball_31,  
  108.             R.drawable.redball_32,  
  109.             R.drawable.redball_33,  
  110.             R.drawable.redball_34,  
  111.             R.drawable.redball_35,  
  112.               
  113.     };  
  114.       
  115.     private Integer[] blueBallIds =   
  116.     {  
  117.         R.drawable.blueball_01,  
  118.         R.drawable.blueball_02,  
  119.         R.drawable.blueball_03,  
  120.         R.drawable.blueball_04,  
  121.         R.drawable.blueball_05,  
  122.         R.drawable.blueball_06,  
  123.         R.drawable.blueball_07,  
  124.         R.drawable.blueball_08,  
  125.         R.drawable.blueball_09,  
  126.         R.drawable.blueball_10,  
  127.         R.drawable.blueball_11,  
  128.         R.drawable.blueball_12,  
  129.         R.drawable.blueball_13,  
  130.         R.drawable.blueball_14,  
  131.         R.drawable.blueball_15,  
  132.         R.drawable.blueball_16,  
  133.     };  
  134.     
  135.     private Integer[] blueBallChangeIds =  
  136.     {  
  137.         R.drawable.blueball_dark01,  
  138.         R.drawable.blueball_dark02,  
  139.         R.drawable.blueball_dark03,  
  140.         R.drawable.blueball_dark04,  
  141.         R.drawable.blueball_dark05,  
  142.         R.drawable.blueball_dark06,  
  143.         R.drawable.blueball_dark07,  
  144.         R.drawable.blueball_dark08,  
  145.         R.drawable.blueball_dark09,  
  146.         R.drawable.blueball_dark10,  
  147.         R.drawable.blueball_dark11,  
  148.         R.drawable.blueball_dark12,  
  149.         R.drawable.blueball_dark13,  
  150.         R.drawable.blueball_dark14,  
  151.         R.drawable.blueball_dark15,  
  152.         R.drawable.blueball_dark16,  
  153.     };  
  154.       
  155.     @Override  
  156.     public void onCreate(Bundle savedInstanceState) {  
  157.         super.onCreate(savedInstanceState);  
  158.         setContentView(R.layout.main);  
  159.         drawBackground();  
  160.         initSelectedBallArrayList();  
  161.         initBall_GridView();  
  162.         initDialog_Button();  
  163.     }  
  164.   
  165.     public void drawBackground()      
  166.              {      
  167.                  GradientDrawable grad = new GradientDrawable(       
  168.                             Orientation.TL_BR,      
  169.                             new int[] {    
  170.                                            Color.rgb(00127),      
  171.                                            Color.rgb(00255),      
  172.                                            Color.rgb(1270255),      
  173.                                            Color.rgb(127127255),      
  174.                                            Color.rgb(127255255),      
  175.                                            Color.rgb(255255255)    
  176.                                        }       
  177.                  );       
  178.                
  179.                  this.getWindow().setBackgroundDrawable(grad);      
  180.              }      
  181.   
  182.       
  183.     private void initSelectedBallArrayList()  
  184.     {  
  185.         for(int i=0;i<35;i++){  
  186.             redBallSelectArr.add(i,0);  
  187.         }  
  188.         for(int i=0;i<16;i++){  
  189.             blueBallSelectArr.add(i,0);  
  190.         }  
  191.     }  
  192.       
  193.     private void initBall_GridView()  
  194.     {  
  195.         showRedGridView = (GridView)findViewById(R.id.show_red_gridview);  
  196.         showBlueGridView = (GridView)findViewById(R.id.show_blue_gridview);  
  197.     }  
  198.       
  199.     private void initDialog_Button()  
  200.     {  
  201.         dialog_button = (Button)findViewById(R.id.dialog_button);  
  202.         dialog_button.setOnClickListener(new OnClickListener(){  
  203.   
  204.             public void onClick(View arg0) {  
  205.                 show_ball_dialog = new AlertDialog.Builder(MainActivity.this).create();  
  206.                 show_ball_dialog_view = View.inflate(MainActivity.this, R.layout.ball_grid_dialog, null);  
  207.                 redBallGrid = (GridView)(show_ball_dialog_view).findViewById(R.id.red_ball_grid);  
  208.                 blueBallGrid = (GridView)(show_ball_dialog_view).findViewById(R.id.blue_ball_grid);  
  209.                 okButton = (Button)(show_ball_dialog_view).findViewById(R.id.Ok_Button);  
  210.                 resetButton = (Button)(show_ball_dialog_view).findViewById(R.id.Reset_Button);  
  211.                   
  212.                 redBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,redBallSelectArr));  
  213.                 blueBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,blueBallSelectArr));  
  214.                 show_ball_dialog.setView(show_ball_dialog_view);  
  215.                 show_ball_dialog.show();  
  216.                   
  217.                 redBallGrid.setOnItemClickListener(new OnItemClickListener(){  
  218.   
  219.                     public void onItemClick(AdapterView<?> arg0, View arg1,  
  220.                             int arg2, long arg3) {  
  221.                         if(redNumLeft > 1){  
  222.                             if(redBallSelectArr.get(arg2) == 0){  
  223.                                 arg1.setBackgroundResource(changeimageid[arg2]);  
  224.                                 redBallSelectArr.set(arg2,arg2+1);  
  225.                                 redNumLeft --;  
  226.                             }  
  227.                             else{  
  228.                                 arg1.setBackgroundResource(mImageIds[arg2]);  
  229.                                 redBallSelectArr.set(arg2, 0);  
  230.                                 redNumLeft += 1;  
  231.                             }  
  232.                         }  
  233.                         else{  
  234.                             if(redBallSelectArr.get(arg2) != 0){  
  235.                                 arg1.setBackgroundResource(mImageIds[arg2]);  
  236.                                 redBallSelectArr.set(arg2, 0);  
  237.                                 redNumLeft +=1;  
  238.                             }else{  
  239.                                 Toast.makeText(MainActivity.this"you choose enough",Toast.LENGTH_SHORT).show();  
  240.                             }  
  241.                         }  
  242.                     }  
  243.                 });  
  244.                   
  245.                 blueBallGrid.setOnItemClickListener(new OnItemClickListener(){  
  246.   
  247.                     public void onItemClick(AdapterView<?> arg0, View arg1,  
  248.                             int arg2, long arg3) {  
  249.                         if(blueNumLeft > 1){  
  250.                             if(blueBallSelectArr.get(arg2) == 0){  
  251.                                 arg1.setBackgroundResource(blueBallChangeIds[arg2]);  
  252.                                 blueBallSelectArr.set(arg2,arg2+1);  
  253.                                 blueNumLeft --;  
  254.                             }  
  255.                             else{  
  256.                                 arg1.setBackgroundResource(blueBallIds[arg2]);  
  257.                                 blueBallSelectArr.set(arg2, 0);  
  258.                                 blueNumLeft += 1;  
  259.                             }  
  260.                         }  
  261.                         else{  
  262.                             if(blueBallSelectArr.get(arg2) != 0){  
  263.                                 arg1.setBackgroundResource(blueBallIds[arg2]);  
  264.                                 blueBallSelectArr.set(arg2, 0);  
  265.                                 blueNumLeft +=1;  
  266.                             }else{  
  267.                                 Toast.makeText(MainActivity.this"you choose enough",Toast.LENGTH_SHORT).show();  
  268.                             }  
  269.                         }  
  270.                     }  
  271.                 });  
  272.                   
  273.                 okButton.setOnClickListener(new OnClickListener(){  
  274.                       
  275.                     public void onClick(View arg0) {  
  276.                         showRedGridView.setAdapter(new ShowBallArrayAdapter(MainActivity.this, MainActivity.this, redBallSelectArr));  
  277.                         showBlueGridView.setAdapter(new ShowBallArrayAdapter(MainActivity.this, MainActivity.this, blueBallSelectArr));  
  278.                         show_ball_dialog.dismiss();  
  279.                     }  
  280.                 });  
  281.                   
  282.                 resetButton.setOnClickListener(new OnClickListener(){  
  283.   
  284.                     public void onClick(View arg0) {  
  285.                         redBallSelectArr.clear();  
  286.                         blueBallSelectArr.clear();  
  287.                         initSelectedBallArrayList();  
  288.                         redNumLeft = 36; blueNumLeft = 16;  
  289.                         redBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,redBallSelectArr));  
  290.                         blueBallGrid.setAdapter(new ImageAdapter(MainActivity.this,MainActivity.this,blueBallSelectArr));  
  291.                     }  
  292.                 });  
  293.             }  
  294.         });  
  295.     }  
  296. }  

 

二、ImageAdapter.java类中的代码:

[java] view plaincopyprint?
  1. package com.cn.daming;  
  2.   
  3.   
  4. import java.util.ArrayList;  
  5.   
  6. import android.content.Context;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.GridView;  
  11. import android.widget.ImageView;  
  12.   
  13. public class ImageAdapter extends BaseAdapter  
  14. {  
  15.     MainActivity activity;  
  16.     private Context     mContext;  
  17.     ImageView imageView;  
  18.     private Integer[]   mImageIds   =   
  19.     {   
  20.             R.drawable.redball_01,  
  21.             R.drawable.redball_02,  
  22.             R.drawable.redball_03,  
  23.             R.drawable.redball_04,  
  24.             R.drawable.redball_05,  
  25.             R.drawable.redball_06,  
  26.             R.drawable.redball_07,  
  27.             R.drawable.redball_08,  
  28.             R.drawable.redball_09,  
  29.             R.drawable.redball_10,  
  30.             R.drawable.redball_11,  
  31.             R.drawable.redball_12,  
  32.             R.drawable.redball_13,  
  33.             R.drawable.redball_14,  
  34.             R.drawable.redball_15,  
  35.             R.drawable.redball_16,  
  36.             R.drawable.redball_17,  
  37.             R.drawable.redball_18,  
  38.             R.drawable.redball_19,  
  39.             R.drawable.redball_20,  
  40.             R.drawable.redball_21,  
  41.             R.drawable.redball_22,  
  42.             R.drawable.redball_23,  
  43.             R.drawable.redball_24,  
  44.             R.drawable.redball_25,  
  45.             R.drawable.redball_26,  
  46.             R.drawable.redball_27,  
  47.             R.drawable.redball_28,  
  48.             R.drawable.redball_29,  
  49.             R.drawable.redball_30,  
  50.             R.drawable.redball_31,  
  51.             R.drawable.redball_32,  
  52.             R.drawable.redball_33,  
  53.             R.drawable.redball_34,  
  54.             R.drawable.redball_35,  
  55.               
  56.     };  
  57.     private Integer[] changeimageid = {  
  58.             R.drawable.redball_dark01,  
  59.             R.drawable.redball_dark02,  
  60.             R.drawable.redball_dark03,  
  61.             R.drawable.redball_dark04,  
  62.             R.drawable.redball_dark05,  
  63.             R.drawable.redball_dark06,  
  64.             R.drawable.redball_dark07,  
  65.             R.drawable.redball_dark08,  
  66.             R.drawable.redball_dark09,  
  67.             R.drawable.redball_dark10,  
  68.             R.drawable.redball_dark11,  
  69.             R.drawable.redball_dark12,  
  70.             R.drawable.redball_dark13,  
  71.             R.drawable.redball_dark14,  
  72.             R.drawable.redball_dark15,  
  73.             R.drawable.redball_dark16,  
  74.             R.drawable.redball_dark17,  
  75.             R.drawable.redball_dark18,  
  76.             R.drawable.redball_dark19,  
  77.             R.drawable.redball_dark20,  
  78.             R.drawable.redball_dark21,  
  79.             R.drawable.redball_dark22,  
  80.             R.drawable.redball_dark23,  
  81.             R.drawable.redball_dark24,  
  82.             R.drawable.redball_dark25,  
  83.             R.drawable.redball_dark26,  
  84.             R.drawable.redball_dark27,  
  85.             R.drawable.redball_dark28,  
  86.             R.drawable.redball_dark29,  
  87.             R.drawable.redball_dark30,  
  88.             R.drawable.redball_dark31,  
  89.             R.drawable.redball_dark32,  
  90.             R.drawable.redball_dark33,  
  91.             R.drawable.redball_dark34,  
  92.             R.drawable.redball_dark35,  
  93.               
  94.     };  
  95.     private Integer[] blueBallIds =   
  96.     {  
  97.         R.drawable.blueball_01,  
  98.         R.drawable.blueball_02,  
  99.         R.drawable.blueball_03,  
  100.         R.drawable.blueball_04,  
  101.         R.drawable.blueball_05,  
  102.         R.drawable.blueball_06,  
  103.         R.drawable.blueball_07,  
  104.         R.drawable.blueball_08,  
  105.         R.drawable.blueball_09,  
  106.         R.drawable.blueball_10,  
  107.         R.drawable.blueball_11,  
  108.         R.drawable.blueball_12,  
  109.         R.drawable.blueball_13,  
  110.         R.drawable.blueball_14,  
  111.         R.drawable.blueball_15,  
  112.         R.drawable.blueball_16,  
  113.     };  
  114. private Integer[] blueBallChangeIds =  
  115.     {  
  116.         R.drawable.blueball_dark01,  
  117.         R.drawable.blueball_dark02,  
  118.         R.drawable.blueball_dark03,  
  119.         R.drawable.blueball_dark04,  
  120.         R.drawable.blueball_dark05,  
  121.         R.drawable.blueball_dark06,  
  122.         R.drawable.blueball_dark07,  
  123.         R.drawable.blueball_dark08,  
  124.         R.drawable.blueball_dark09,  
  125.         R.drawable.blueball_dark10,  
  126.         R.drawable.blueball_dark11,  
  127.         R.drawable.blueball_dark12,  
  128.         R.drawable.blueball_dark13,  
  129.         R.drawable.blueball_dark14,  
  130.         R.drawable.blueball_dark15,  
  131.         R.drawable.blueball_dark16,  
  132.     };  
  133.     private ArrayList<Integer> filter = new ArrayList<Integer>();  
  134.   
  135.     public ImageAdapter(Context c,MainActivity a ,ArrayList<Integer> ballAL )  
  136.     {  
  137.         activity = a;  
  138.         mContext = c;  
  139.         filter = ballAL;  
  140.     }  
  141.       
  142.     public int getCount()  
  143.     {  
  144.         if (filter.size() == 35){  
  145.             return 35;  
  146.         }  
  147.         else{  
  148.             return 16;  
  149.         }  
  150.     }  
  151.   
  152.     public Object getItem(int position)  
  153.     {  
  154.         return position;  
  155.     }  
  156.   
  157.     public long getItemId(int position)  
  158.     {  
  159.         return position;  
  160.     }  
  161.   
  162.   
  163.     public View getView(final int position, View convertView, ViewGroup parent)  
  164.     {  
  165.         if (convertView == null)  
  166.         {  
  167.             imageView = new ImageView(mContext);  
  168.             imageView.setLayoutParams(new GridView.LayoutParams(2424));  
  169.             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  170.         }  
  171.         else  
  172.         {  
  173.             imageView = (ImageView) convertView;  
  174.         }  
  175.         if (filter.get(position) == 0 && filter.size()== 35){  
  176.             imageView.setBackgroundResource(mImageIds[position]);  
  177.         }  
  178.         else if (filter.get(position) != 0 && filter.size()== 35){  
  179.             imageView.setBackgroundResource(changeimageid[position]);  
  180.         }  
  181.         else if (filter.get(position) == 0 && filter.size() == 16){  
  182.             imageView.setBackgroundResource(blueBallIds[position]);  
  183.         }  
  184.       
  185.         else if (filter.get(position) != 0 && filter.size() == 16){  
  186.             imageView.setBackgroundResource(blueBallChangeIds[position]);  
  187.         }  
  188.           
  189.         return imageView;  
  190.     }  
  191.   
  192. }  

 

三、ShowBallArrayAdapter.java类中的代码:

[java] view plaincopyprint?
  1. package com.cn.daming;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.content.Context;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.GridView;  
  10. import android.widget.ImageView;  
  11.   
  12. public class ShowBallArrayAdapter extends BaseAdapter {  
  13.     MainActivity activity;  
  14.     private ArrayList<Integer> filterresult;  
  15.     private Context mContext;  
  16.     ImageView imageView;  
  17.     private int[] imageI;  
  18.     private Integer[] mImageIds = { R.drawable.redball_01,  
  19.             R.drawable.redball_02, R.drawable.redball_03,  
  20.             R.drawable.redball_04, R.drawable.redball_05,  
  21.             R.drawable.redball_06, R.drawable.redball_07,  
  22.             R.drawable.redball_08, R.drawable.redball_09,  
  23.             R.drawable.redball_10, R.drawable.redball_11,  
  24.             R.drawable.redball_12, R.drawable.redball_13,  
  25.             R.drawable.redball_14, R.drawable.redball_15,  
  26.             R.drawable.redball_16, R.drawable.redball_17,  
  27.             R.drawable.redball_18, R.drawable.redball_19,  
  28.             R.drawable.redball_20, R.drawable.redball_21,  
  29.             R.drawable.redball_22, R.drawable.redball_23,  
  30.             R.drawable.redball_24, R.drawable.redball_25,  
  31.             R.drawable.redball_26, R.drawable.redball_27,  
  32.             R.drawable.redball_28, R.drawable.redball_29,  
  33.             R.drawable.redball_30, R.drawable.redball_31,  
  34.             R.drawable.redball_32, R.drawable.redball_33,   
  35.             R.drawable.redball_34,R.drawable.redball_35,};  
  36.     private Integer[] blueImageIds = { R.drawable.blueball_01,  
  37.             R.drawable.blueball_02, R.drawable.blueball_03,  
  38.             R.drawable.blueball_04, R.drawable.blueball_05,  
  39.             R.drawable.blueball_06, R.drawable.blueball_07,  
  40.             R.drawable.blueball_08, R.drawable.blueball_09,  
  41.             R.drawable.blueball_10, R.drawable.blueball_11,  
  42.             R.drawable.blueball_12, R.drawable.blueball_13,  
  43.             R.drawable.blueball_14, R.drawable.blueball_15,  
  44.             R.drawable.blueball_16, };  
  45.   
  46.     StringBuffer sb = new StringBuffer();  
  47.     String[] temStr;  
  48.     Integer j = 0;  
  49.     String tempS;  
  50.   
  51.     public ShowBallArrayAdapter(Context c, MainActivity at, ArrayList<Integer> al) {  
  52.         mContext = c;  
  53.         activity = at;  
  54.         filterresult = al;  
  55.         for (int i = 0; i < filterresult.size(); i++) {  
  56.             if (filterresult.get(i) != 0) {  
  57.                 sb.append(filterresult.get(i) + ",");  
  58.             }  
  59.         }  
  60.         ;  
  61.         String s = sb.toString();  
  62.         if (s.length() != 0) {  
  63.             tempS = s.substring(0, s.length() - 1);  
  64.             temStr = tempS.split(",");  
  65.             imageI = new int[temStr.length];  
  66.             for (int i = 0; i < temStr.length; i++) {  
  67.                 j = Integer.parseInt(temStr[i]);  
  68.                 imageI[i] = j - 1;  
  69.             }  
  70.               
  71.             for(int k=0;k<temStr.length;k++){  
  72.                 if (filterresult.size() == 35){  
  73.                     imageI[k] = mImageIds[imageI[k]];  
  74.                 }  
  75.                 else if (filterresult.size() == 16){  
  76.                     imageI[k] = blueImageIds[imageI[k]];     
  77.                 }  
  78.             }  
  79.         } else {  
  80.             tempS = s.substring(0, s.length());  
  81.         }  
  82.   
  83.     }  
  84.   
  85.     public int getCount() {  
  86.         if (temStr == null) {  
  87.             return 0;  
  88.         } else {  
  89.             return temStr.length;  
  90.         }  
  91.     }  
  92.   
  93.     public Object getItem(int position) {  
  94.         return position;  
  95.     }  
  96.   
  97.     public long getItemId(int position) {  
  98.         return position;  
  99.     }  
  100.   
  101.     public View getView(int position, View convertView, ViewGroup parent) {  
  102.         if (convertView == null) {  
  103.             imageView = new ImageView(mContext);  
  104.             imageView.setClickable(true);  
  105.             imageView.setLayoutParams(new GridView.LayoutParams(2424));  
  106.             imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
  107.         } else {  
  108.             imageView = (ImageView) convertView;  
  109.         }  
  110.   
  111.         imageView.setBackgroundResource(imageI[position]);  
  112.         return imageView;  
  113.     }  
  114.   
  115. }  

 

Layout布局中的文件:

一、main.xml布局文件中的代码:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.    <View   
  8.         android:layout_marginLeft="1dip"  
  9.         android:layout_width="2dip"   
  10.         android:layout_height="fill_parent"  
  11.         android:background="#FF909090" />  
  12.    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  13.         android:orientation="vertical"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="fill_parent"  
  16.        >  
  17.     <View   
  18.         android:layout_width="fill_parent"   
  19.         android:layout_height="2dip"  
  20.         android:background="#FF909090" />  
  21.     <TextView    
  22.         android:layout_width="fill_parent"   
  23.         android:layout_height="wrap_content"   
  24.         android:gravity="center_horizontal|center_vertical"  
  25.         android:text="@string/hello"  
  26.         android:textSize="10pt"  
  27.         android:textColor="#FF000000"  
  28.      />  
  29.      <View   
  30.         android:layout_width="fill_parent"   
  31.         android:layout_height="2dip"  
  32.         android:background="#FF909090" />  
  33.      <Button  
  34.         android:id="@+id/dialog_button"  
  35.         android:layout_width="fill_parent"  
  36.         android:layout_height="wrap_content"  
  37.         android:text="点击弹出Dialog对话框"  
  38.         android:textSize="10pt"  
  39.      />  
  40.      <View   
  41.         android:layout_width="fill_parent"   
  42.         android:layout_height="2dip"  
  43.         android:background="#FF909090" />  
  44.      <TextView  
  45.         android:layout_width="fill_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:gravity="center_horizontal|center_vertical"  
  48.         android:text="红球选中球数"  
  49.         android:textSize="10pt"  
  50.         android:textColor="#FF000000"  
  51.      />  
  52.      <GridView  
  53.         android:id="@+id/show_red_gridview"  
  54.         android:layout_width="match_parent"  
  55.         android:layout_height="wrap_content"  
  56.         android:horizontalSpacing="6dip"  
  57.         android:verticalSpacing="16dip"  
  58.         android:numColumns="8"  
  59.         android:gravity="center"  
  60.     />  
  61.     <View   
  62.         android:layout_width="fill_parent"   
  63.         android:layout_height="2dip"  
  64.         android:background="#FF909090" />  
  65.     <TextView  
  66.         android:layout_width="fill_parent"  
  67.         android:layout_height="wrap_content"  
  68.         android:gravity="center_horizontal|center_vertical"  
  69.         android:text="蓝球选中球数"  
  70.         android:textSize="10pt"  
  71.         android:textColor="#FF000000"  
  72.      />  
  73.      <GridView  
  74.         android:id="@+id/show_blue_gridview"  
  75.         android:layout_width="match_parent"  
  76.         android:layout_height="wrap_content"  
  77.         android:horizontalSpacing="6dip"  
  78.         android:verticalSpacing="16dip"  
  79.         android:numColumns="8"  
  80.         android:gravity="center"  
  81.     />  
  82.      <View   
  83.         android:layout_marginBottom="2dip"  
  84.         android:layout_width="fill_parent"   
  85.         android:layout_height="2dip"  
  86.         android:background="#FF909090" />  
  87.   </LinearLayout>  
  88.   <View   
  89.         android:layout_marginRight="2dip"  
  90.         android:layout_width="2dip"   
  91.         android:layout_height="fill_parent"  
  92.         android:background="#FF909090" />  
  93. </LinearLayout>  

 

二、ball_grid_dialog.xml布局文件中的代码:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.      <TextView  
  8.        android:id="@+id/gridviewtitle"  
  9.        android:layout_width="fill_parent"  
  10.        android:layout_height="wrap_content"  
  11.        android:gravity="center_horizontal"  
  12.        android:text="@string/ball_textview"  
  13.        android:textSize="20dip"  
  14.        android:background="#00000000"  
  15.        />  
  16.     <View   
  17.         android:layout_width="fill_parent"   
  18.         android:layout_height="2dip"  
  19.         android:background="#FF909090" />  
  20.     <GridView   
  21.         android:id="@+id/red_ball_grid"   
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:horizontalSpacing="5dip"  
  25.         android:verticalSpacing="10dip"  
  26.         android:numColumns="7"  
  27.         android:gravity="center" />  
  28.     <View   
  29.         android:layout_width="fill_parent"   
  30.         android:layout_height="2dip"  
  31.         android:background="#FF909090" />  
  32.     <GridView   
  33.         android:id="@+id/blue_ball_grid"  
  34.         android:layout_width="match_parent"   
  35.         android:layout_height="wrap_content"  
  36.         android:horizontalSpacing="5dip"  
  37.         android:verticalSpacing="10dip"  
  38.         android:numColumns="7"  
  39.         android:gravity="center" />  
  40.     <View   
  41.         android:layout_width="fill_parent"  
  42.         android:layout_height="2dip"  
  43.         android:background="#FF909090" />  
  44.   
  45.     <RelativeLayout   
  46.         android:layout_width="fill_parent"  
  47.         android:layout_height="wrap_content">  
  48.         <Button   
  49.             android:id="@+id/Ok_Button"   
  50.             android:text="@string/ok_button"  
  51.             android:layout_width="wrap_content"   
  52.             android:layout_height="wrap_content"  
  53.             android:layout_alignParentRight="true"/>  
  54.         <Button   
  55.             android:id="@+id/Reset_Button"   
  56.             android:text="@string/reset_button"  
  57.             android:layout_width="wrap_content"  
  58.             android:layout_height="wrap_content"  
  59.             android:layout_toLeftOf="@id/Ok_Button">  
  60.         </Button>  
  61.     </RelativeLayout>  
  62. </LinearLayout>  

 

values目录下的string.xml中的代码:        

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">大明动态改变GridView的值!</string>  
  4.     <string name="app_name">大明动态GridViewDemo</string>  
  5.     <string name="ball_textview">大明选中球界面</string>  
  6.     <string name="ok_button">确定</string>  
  7.     <string name="reset_button">重置</string>  
  8. </resources>  


AndroidManifest.xml中的代码:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cn.daming"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest>  


       

0 0
原创粉丝点击