无法解析构造函数Intent和ArrayAdapter

时间:2017-08-07 12:06:33

标签: java android android-intent android-arrayadapter

这是我的代码:

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();

来保存

2 个答案:

答案 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