这是我的代码:
行
ArrayAdapter<Diseases>(this , android.R.layout.simple_list_item_1, data);
和
Intent intent = new Intent(this, DetailActivity.class);
显示错误
无法解析构造函数 &#39; ArrayAdapter(com.android.mahanr.healthapp.Tab1,int, java.util.List中)&#39;
和
无法解析构造函数&#39; Intent(com.android.mahanr.healthapp.Tab1, java.lang.Class中)&#39;
如何使用getActivity()
代替data = DataProviderDiseases.getData();
答案 0 :(得分:0)
这是一个片段,你应该使用 getActivity()
而不是 this
。
第1步
ArrayAdapter<Diseases> diseasesArrayAdapter
= new ArrayAdapter<Diseases>(getActivity(), android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) listView.findViewById(android.R.id.list);
第2步
private void displayDetail(Diseases diseases) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra(DISEASES_TITLE, diseases.getTitle());
intent.putExtra(DISEASES_DESC, diseases.getDescription());
startActivityForResult(intent, DETAIL_REQUEST_CODE);
}
第3步
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_diseases_main, container, false);
data = DataProviderDiseases.getData();
ArrayAdapter<Diseases> diseasesArrayAdapter= new ArrayAdapter<Diseases>(getActivity(), android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setAdapter(diseasesArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Diseases diseases = data.get(position);
displayDetail(diseases);
}
});
return rootView; // It must be here
}
答案 1 :(得分:0)
ArrayAdapter构造函数的任何变体都需要Context
作为第一个参数。在初始化getContext()
实例期间,请使用this
代替ArrayAdapter
。