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