我正在尝试使用自定义适配器填充列表视图,但我得到了 以下错误“App.this无法从静态上下文引用”。
问题不在于“getActivity()”,而是从静态上下文调用CustomAdapter本身。这样做的正确方法是什么?
public class App extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//
}
static public class PlanetFragment extends Fragment {
ListView lv;
String planet=null;
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv.setAdapter(new CustomAdapter(getActivity(), savedInstanceState, planet));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
lv = (ListView) rootView.findViewById(R.id.list);
}
}
}
class CustomAdapter extends BaseAdapter
{
//Some access to context and operations etc is done to populate the rows
}
}