Android中,Context是一种抽象类,它直接继承了Object,它由Android系统来实现,它可以得到一个应用程序的运行环境。但只能在activity,broadcasting,等中获得,如果要在其他一般类中使用context,需要进行传递。

先看看之前写的一段代码:

public class MainActivity extends AppCompatActivity {    private ListView lv;    private BaseAdapter adapter;    class MyAdapter extends BaseAdapter {        private CoustomListCellData[] data = new CoustomListCellData[]{                new CoustomListCellData("英雄联盟", "《英雄联盟》(简称lol)是由美国Riot Games开发,中国大陆地区由腾讯游戏运营的网络游戏。", R.drawable.img1),                new CoustomListCellData("反恐精英:全球攻势", "《反恐精英:全球攻势》是一款由VALVE与Hidden Path Entertainment合作开发的第一人称射击游戏,于2012年8月21日在欧美地区正式发售,为《反恐精英》系列游戏的第四款作品", R.drawable.img2),                new CoustomListCellData("守望先锋", "《守望先锋》(Overwatch,简称OW) 是由暴雪娱乐公司开发的一款第一人称射击游戏,于2016年5月24日全球上市,中国大陆地区由网易公司代理。", R.drawable.img3)        };        //     private String[] data =new String[]{"SB1","SB2","SB3","SB4","SB5","SB6","SB7","SB8","SB9","SB10","SB11","SB12","SB13"};        @Override        public int getCount() {            return data.length;        }        @Override        public CoustomListCellData getItem(int position) {            return data[position];        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            CoustomListCellData data = getItem(position);            LinearLayout ll = null;            if (convertView == null) {                ll = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.custom_listcell, null);//*******************            } else {                ll = (LinearLayout) convertView;            }            ImageView icon = (ImageView) ll.findViewById(R.id.icon);            TextView name = (TextView) ll.findViewById(R.id.name);            TextView decription = (TextView) ll.findViewById(R.id.description);            name.setTextColor(Color.BLACK);            decription.setTextColor(Color.BLACK);            icon.setImageResource(data.iconId);            name.setText(data.name);            decription.setText(data.description);            return ll;        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv = (ListView) findViewById(R.id.listView);        adapter = new MyAdapter();        lv.setAdapter(adapter);    }}

在这里,由于打*号的语句中需要用到activity的上下文,但由于这个Adapter的代码很长,我们就想要把它分离到单独的一个类里。但是要注意Context只能在activity等中获得,所以要在构造方法中传一个context进去,相应的adapter类也需要多一个context属性。

分离后代码如下:
Adapter类

public class MyAdapter extends BaseAdapter {    private CoustomListCellData[] data = new CoustomListCellData[]{            new CoustomListCellData("英雄联盟", "《英雄联盟》(简称lol)是由美国Riot Games开发,中国大陆地区由腾讯游戏运营的网络游戏。", R.drawable.img1),            new CoustomListCellData("反恐精英:全球攻势", "《反恐精英:全球攻势》是一款由VALVE与Hidden Path Entertainment合作开发的第一人称射击游戏,于2012年8月21日在欧美地区正式发售,为《反恐精英》系列游戏的第四款作品", R.drawable.img2),            new CoustomListCellData("守望先锋", "《守望先锋》(Overwatch,简称OW) 是由暴雪娱乐公司开发的一款第一人称射击游戏,于2016年5月24日全球上市,中国大陆地区由网易公司代理。", R.drawable.img3)    };    //     private String[] data =new String[]{"SB1","SB2","SB3","SB4","SB5","SB6","SB7","SB8","SB9","SB10","SB11","SB12","SB13"};    Context context;    public MyAdapter(Context context) {        this.context=context;    }    @Override    public int getCount() {        return data.length;    }    @Override    public CoustomListCellData getItem(int position) {        return data[position];    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        //      System.out.println(position);        //    System.out.println(">>>>>>>>>>>");        //  TextView tv;        //     if (convertView==null)        //      {        //           tv=new TextView(getApplicationContext());        //   System.out.println(">>>>>>>>>>>");        //      }        //        else        //        {        //         tv=(TextView) convertView;        //   System.out.println("<<<<<<<<<<<");        //    }        //     tv.setTextSize(50);        //      tv.setTextColor(Color.BLACK);        //      tv.setText(getItem(position));        //      return tv;        CoustomListCellData data = getItem(position);        LinearLayout ll = null;        if (convertView == null) {            ll = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.custom_listcell, null);        } else {            ll = (LinearLayout) convertView;        }        ImageView icon = (ImageView) ll.findViewById(R.id.icon);        TextView name = (TextView) ll.findViewById(R.id.name);        TextView decription = (TextView) ll.findViewById(R.id.description);        name.setTextColor(Color.BLACK);        decription.setTextColor(Color.BLACK);        icon.setImageResource(data.iconId);        name.setText(data.name);        decription.setText(data.description);        return ll;    }}

main_activity类:

public class MainActivity extends AppCompatActivity {    private ListView lv;    private BaseAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        lv = (ListView) findViewById(R.id.listView);        adapter = new MyAdapter(getApplicationContext());        lv.setAdapter(adapter);    }}

context的官方文档:
https://developer.android.com/reference/android/content/Context.html

更多相关文章

  1. Android(安卓)Studio 之 对话框开发(Dialog) ---- 两种方式来实
  2. 一网打尽__Android(安卓)开源代码合集(WebView框架)
  3. Android(安卓)开发之录音与播放
  4. Android(安卓)联系人开发- 保存联系人
  5. Android(安卓)UI开发第十一篇――右上角带个泡泡
  6. Mac 进行 android 真机调试
  7. Android(安卓)开发资料汇总
  8. android 点击5次打开开发者模式
  9. Android(安卓)App项目开发步骤小结

随机推荐

  1. Android——SharedPreferences数据存储
  2. Android(安卓)kernel Download
  3. Android——httpPost方式网络互通信
  4. Android游戏设计之-------游戏音效的播放
  5. [置顶] Android(安卓)ButterKnife
  6. android 快速开发(三)巧用公共标题栏:避免每
  7. android webview 介绍
  8. Windows7 Android(安卓)开发环境搭建
  9. Android(安卓)adb启动任意app的几种方式
  10. Retrofit