在我新建的adapter中‘MainActivity.this’变红,有错误提示:com.e.myapplication1.MainActivity’ is not an enclosing class,我尝试将’MainActivity.this’改成’MainActivity.class’,也有同样的错误提示
代码如下:
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return DATA.length;
}
@Override
public Object getItem(int position) {
return DATA[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(
MainActivity.this);
}
TextView view = (TextView) convertView;
view.setText(DATA[position]);
view.setBackgroundColor(COLOR[position % 5]);
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
view.setGravity(Gravity.BOTTOM);
view.setTextColor(Color.WHITE);
return view;
}
private static final String[] DATA = new String[] {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
"Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
"Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
"Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
"Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss",
"Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
"Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase",
"Baylough", "Beaufort", "Beauvoorde"
};
private static final int[] COLOR = new int[] {
0xff33b5e5, 0xffaa66cc, 0xff99cc00, 0xffffbb33, 0xffff4444
};
}
有遇到类似问题的道友们可以帮助我解决这个问题吗,万分感谢!