2014-02-27 136 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范围不限制从顶级类内部访问您的类,甚至是从其他内部/嵌套类访问您的类。“源文件”中的内容可以看到您的类/字段/方法...)

如果您通过反射使用该方法,可能会发生警告错误。由于这只是一个警告,你应该做的是:确保你的代码是正确的,然后忽略警告。或者了解你的代码错在哪里并修复它。