2015-10-07 35 views
2

剛剛接觸到android,我正在嘗試創建一個應用程序,該應用程序將根據用戶的狀態在正在使用帶抽象碎片的導航抽屜中顯示人員。CustomListAdapter中的CustomListAdapter()無法應用

當我試圖列出特定類別的人的時候,我得到了一個錯誤。即

Error:(50, 57) error: incompatible types: Past cannot be converted to Activity 
Note: Some input files use or override a deprecated API. 
Note: Recompile with -Xlint:deprecation for details. 
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. 

Compilation failed; see the compiler error output for details.

我得到錯誤 「CustomListAdapter()中CustomListAdapter不能應用於」 下方

從MainActivity稱爲
import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class CustomListAdapter extends ArrayAdapter<String> { 

private final Activity context; 
private final String[] itemname; 
private final Integer[] imgid; 

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) { 
    super(context, R.layout.mylist, itemname); 
    // TODO Auto-generated constructor stub 

    this.context=context; 
    this.itemname=itemname; 
    this.imgid=imgid; 
} 

public View getView(int position,View view,ViewGroup parent) { 
    LayoutInflater inflater=context.getLayoutInflater(); 
    View rowView=inflater.inflate(R.layout.mylist, null,true); 

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1); 

    txtTitle.setText(itemname[position]); 
    imageView.setImageResource(imgid[position]); 
    extratxt.setText("Description "+itemname[position]); 
    return rowView; 

    }; 
} 

片段

import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ListView; 


    public class Past extends Fragment { 

ListView list; 
String[] itemname ={ 
     "John", 
     "Mike", 
     "Ria", 
     "jack", 
     "leo", 

}; 

Integer[] imgid={ 
     R.drawable.pic1, 
     R.drawable.pic2, 
     R.drawable.pic3, 
     R.drawable.pic4, 
     R.drawable.pic5, 
}; 

public Past() { 
    // Required empty public constructor 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    return inflater.inflate(R.layout.fragment_past, container, false); 

    CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid); 
     //here am getting an error 
    list=(ListView)getView().findViewById(R.id.list); 
    list.setAdapter(adapter); 

    /*list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      // TODO Auto-generated method stub 
      String Slecteditem= itemname[+position]; 
      Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show(); 

     } 
    });*/ 
} 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 
} 

的CustomListAdapter.java代碼給出以下給出

 else if(i==1){ 
     switch (j){ 

      case 0: 
       Past past = new Past(); 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frameholde,past); 
       fragmentTransaction.commit(); 
       break; 

      case 1: 
       Current current = new Current(); 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frameholde, current); 
       fragmentTransaction.commit(); 
       break; 

      case 2: 
       Area area = new Area(); 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       fragmentTransaction.replace(R.id.frameholde, area); 
       fragmentTransaction.commit(); 
       break; 

     } 
+0

你傳入片段上下文活動上下文而初始化適配器即CustomListAdapter adapter = new CustomListAdapter(this,itemname,imgid);嘗試將其更改爲CustomListAdapter adapter = new CustomListAdapter(getActivity(),itemname,imgid);並嘗試 – Raghavendra

+0

我已經試過它給出了一個不可測量的統計錯誤 – kiki

+0

你發佈的日誌說這個錯誤,所以這個更改更新日誌。 – Raghavendra

回答

1

除了註釋中指出的問題之外,您在設置適配器之前從您的onCreateView()方法返回,因此無法訪問的代碼問題。

設置爲充氣查看的引用,使用該視圖以發現的ListView,然後在該方法的最後返回視圖:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_past, container, false); //modified 

     CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid); 

     list=(ListView) rootView.findViewById(R.id.list); //modified 
     list.setAdapter(adapter); 

     return rootView; //added 
    } 
+0

感謝它的工作......非常感謝你 – kiki

+0

我有同樣的問題,但IDE不支持getActivity。 –

0

通過this.getActivity()而不是this。如果仍然出現錯誤,請用新錯誤更新您的問題。

+0

編輯後,我得到了同樣的錯誤「無法訪問聲明「 錯誤:(47,27)錯誤:無法訪問聲明 錯誤:(62,5)錯誤:缺少return語句 注:某些輸入文件使用或覆蓋棄用的API 。 注:重新編譯-Xlint:棄用的詳細信息 錯誤:執行失敗的任務「:應用程序:compileDebugJavaWithJavac」 >編譯失敗;詳見編譯器錯誤輸出 – kiki

+0

@kiki發佈修改後的代碼 – Raghavendra

+0

@Raghavendra。問題解決了。感謝支持手段很多 – kiki

相關問題