2014-02-27 100 views
-1

似乎無法從類私有類陣列適配器警告

public class MainActivity extends Activity { 

EditText nameTxt, ageTxt, nationalityTxt, genderTxt, icnuTxt, dobTxt, pobTxt, heightTxt, weightTxt, bloodTypeTxt, bloodPressureTxt; 
ArrayList<PatientDetails> Details = new ArrayList<PatientDetails>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    private class PatientDetailsAdapter extends ArrayAdapter<PatientDetails>{ 
    public PatientDetailsAdapter(){ 
     super(MainActivity.this, R.layout.details_list, Details); 


    } 
    @Override 
    public View getView(int position, View view, ViewGroup parent){ 
     if (view == null) 
      view = getLayoutInflater().inflate(R.layout.details_list, parent, false); 

     PatientDetails currentPatientDetails = Details.get(position); 

     TextView name = (TextView) view.findViewById(R.id.patientName); 
     name.setText(currentPatientDetails.getName()); 

     TextView age = (TextView) view.findViewById(R.id.patientAge); 
     age.setText(currentPatientDetails.getAge()); 

     TextView icnu = (TextView) view.findViewById(R.id.patientIcnu); 
     icnu.setText(currentPatientDetails.getIcnu()); 

     TextView gender = (TextView) view.findViewById(R.id.patientGender); 
     gender.setText(currentPatientDetails.getGender()); 

     TextView nationality = (TextView) view.findViewById(R.id.patientNationality); 
     nationality.setText(currentPatientDetails.getNationality()); 

     TextView dob = (TextView) view.findViewById(R.id.patientDob); 
     dob.setText(currentPatientDetails.getDob()); 

     TextView pob = (TextView) view.findViewById(R.id.patientPob); 
     pob.setText(currentPatientDetails.getPob()); 

     TextView height = (TextView) view.findViewById(R.id.patientHeight); 
     height.setText(currentPatientDetails.getHeight()); 

     TextView weight = (TextView) view.findViewById(R.id.patientWeight); 
     weight.setText(currentPatientDetails.getWeight()); 

     TextView bloodType = (TextView) view.findViewById(R.id.patientBloodType); 
     bloodType.setText(currentPatientDetails.getBloodType()); 

     TextView bloodPressure = (TextView) view.findViewById(R.id.patientBloodPressure); 
     bloodPressure.setText(currentPatientDetails.getBloodPressure()); 

     return view; 




    } 
} 

刪除警告它詳細介紹了PatientDetailsAdapter黃色下劃線並說:

類型MainActivity.PatientListAdapter是從未在本地使用

我已經添加了公共類活動以及一些私人類 其應用程序添加患者詳細信息和詳細信息列表

+0

您是否創建了該類的一個實例?我嘗試創建私人課程時遇到錯誤,因爲如果它是私人課程,您將如何使用它。 – kai

+2

一個帶有公共構造函數的私有類? – donfuxx

+0

發佈更多的代碼你試過的東西.... –

回答

0

私人類,意味着它只能由其構造的類訪問,並且不能被其他類訪問。

您正在從編輯器收到警告,因爲您已經定義了該類,但從未使用它。

如果您在代碼中使用類,像

PatientDetailsAdapter myAdapter = new PatientDetailsAdapter(); 

你會看到,你不會得到警告了。

當然,如果你打算從另一個類使用ArrayAdapter,你應該公開它。

這是一個正確的適配器的樣本骨架(回答您的其他問題):

private class PatientDetailsAdapter extends ArrayAdapter<PatientDetails> { 

     public PatientDetailsAdapter(Context c, ArrayList<PatientDetails> array) { 
      super(c, 0, array); 
     } 

     @Override 
     public View getView(int position, View view, ViewGroup parent) { 
      if (view == null) 
       view = getLayoutInflater().inflate(R.layout.details_list, parent, false); 

      PatientDetails currentPatientDetails = get(position); 

      TextView name = (TextView) view.findViewById(R.id.patientName); 
      name.setText(currentPatientDetails.getName()); 

      TextView age = (TextView) view.findViewById(R.id.patientAge); 
      age.setText(currentPatientDetails.getAge()); 

      TextView icnu = (TextView) view.findViewById(R.id.patientIcnu); 
      icnu.setText(currentPatientDetails.getIcnu()); 

      TextView gender = (TextView) view.findViewById(R.id.patientGender); 
      gender.setText(currentPatientDetails.getGender()); 

      TextView nationality = (TextView) view.findViewById(R.id.patientNationality); 
      nationality.setText(currentPatientDetails.getNationality()); 

      TextView dob = (TextView) view.findViewById(R.id.patientDob); 
      dob.setText(currentPatientDetails.getDob()); 

      TextView pob = (TextView) view.findViewById(R.id.patientPob); 
      pob.setText(currentPatientDetails.getPob()); 

      TextView height = (TextView) view.findViewById(R.id.patientHeight); 
      height.setText(currentPatientDetails.getHeight()); 

      TextView weight = (TextView) view.findViewById(R.id.patientWeight); 
      weight.setText(currentPatientDetails.getWeight()); 

      TextView bloodType = (TextView) view.findViewById(R.id.patientBloodType); 
      bloodType.setText(currentPatientDetails.getBloodType()); 

      TextView bloodPressure = (TextView) view.findViewById(R.id.patientBloodPressure); 
      bloodPressure.setText(currentPatientDetails.getBloodPressure()); 

      return view; 
     } 
    } 

你初始化適配這種方式(後您的詳細信息ArrayList中有項):

PatientDetailsAdapter myAdapter = new PatientDetailsAdapter(MainActivity.this, Details); 
ListView mList = (ListView) findViewById(R.id.lst); 
mList.setAdapter(myAdapter); 

一旦適配器初始化並設置爲ListView,那麼只要您希望更改ListView中的某些內容,您就會直接向適配器添加/刪除對象,而不是Details ArrayList。

像:myAdapter.add(new PatientDetails(....));myAdapter.remove((PatientDetails)object);

當然notifiy您的ListView而且該數據已經通過調用myAdapter.notifyDataSetChanged();

+0

沒有更多的警告howerever我無法運行它。 –

+0

然後這是另一個問題。你是什​​麼意思,你無法運行它?該適配器需要傳遞ArrayList才能工作。 – Lefteris

+0

明白了。將嘗試調試它。 –

0

PatientDetailsAdapter類肯定是一個內部類改變。頂級課程不能是私人課程。如果您將關鍵字private應用於頂級課程,您將收到編譯錯誤,而不僅僅是警告。

由於您沒有提供包含您的PatientDetailsAdapter的整個類,我只能猜測問題是您沒有在頂級類中或任何其他頂級內部或嵌套類中明確使用您的類高級班。這肯定會導致警告。 (請注意,private範圍不限制從頂級類內部訪問您的類,甚至是從其他內部/嵌套類訪問您的類。「源文件」中的內容可以看到您的類/字段/方法...)

如果您通過反射使用該方法,可能會發生警告錯誤。由於這只是一個警告,你應該做的是:確保你的代碼是正確的,然後忽略警告。或者瞭解你的代碼錯在哪裏並修復它。