权限---根据传递不用的值,显示不同的图标

package com.example.textdemo2;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private GridView mGridView;
    private ArrayList<SudokuCell> list = new ArrayList<MainActivity.SudokuCell>();
    private int ageData[] = new int[] { 0, 2, 7};
    
    // 九宫格显示的名字和对应的ID
    private class SudokuCell {
        public String name;
        public int rId;
    }
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mGridView = (GridView) this.findViewById(R.id.gridView1);
        
        
        for (int i = 0; i < ageData.length; i++) { 
            SudokuCell sudokuCell = new SudokuCell();
            sudokuCell.rId = ageData[i]; 
            if (ageData[i] == 0) {
                sudokuCell.rId = R.drawable.a0;
                sudokuCell.name = "000";
            } else if (ageData[i] == 1) {
                sudokuCell.rId = R.drawable.a1;
                sudokuCell.name = "111";
            } else if (ageData[i] == 2) {
                sudokuCell.rId = R.drawable.a2;
                sudokuCell.name = "222";
            }else if (ageData[i] == 3) {
                sudokuCell.rId = R.drawable.a3;
                sudokuCell.name = "333";
            }else if (ageData[i] == 4) {
                sudokuCell.rId = R.drawable.a4;
                sudokuCell.name = "444";
            }else if (ageData[i] == 5) {
                sudokuCell.rId = R.drawable.a5;
                sudokuCell.name = "555";
            }else if (ageData[i] == 6) {
                sudokuCell.rId = R.drawable.a6;
                sudokuCell.name = "666";
            }else if (ageData[i] == 7) {
                sudokuCell.rId = R.drawable.a7;
                sudokuCell.name = "777";
            }
            list.add(sudokuCell);
        }
        
        mGridView.setAdapter(new MyAdapter());
        mGridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                int resId = list.get(position).rId;

                if (resId == R.drawable.a4) {
                    Toast.makeText(getBaseContext(), "444", 2).show();
                } else if (resId == R.drawable.a0) {
                    Toast.makeText(getBaseContext(), "000", 2).show();
                } else if (resId == R.drawable.a1) {
                    Toast.makeText(getBaseContext(), "111", 2).show();
                } else if (resId == R.drawable.a2) {
                    Toast.makeText(getBaseContext(), "222", 2).show();
                } else if (resId == R.drawable.a3) {
                    Toast.makeText(getBaseContext(), "333", 2).show();
                } else if (resId == R.drawable.a5) {
                    Toast.makeText(getBaseContext(), "555", 2).show();
                } else if (resId == R.drawable.a6) {
                    Toast.makeText(getBaseContext(), "666", 2).show();
                } else if (resId == R.drawable.a7) {
                    Toast.makeText(getBaseContext(), "777", 2).show();
                }
            }
        });
    }
    
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(
                        R.layout.fragment_mainpanel_item, null);
            }
            TextView txtName = (TextView) convertView
                    .findViewById(R.id.textView1);
            txtName.setCompoundDrawablesWithIntrinsicBounds(0,
                    list.get(position).rId, 0, 0);

            txtName.setText(list.get(position).name);

            return convertView;
        }

    }

 
}
原文地址:https://www.cnblogs.com/androidsj/p/3236683.html