私有类数组适配器警告

时间:2014-02-27 08:14:00

标签: android android-arrayadapter

似乎无法从类中删除警告

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永远不会在本地使用

我添加了公共类活动以及私有类的一些活动 它是一个应用程序,用于添加患者详细信息和详细信息列表

2 个答案:

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

并且以这种方式初始化适配器(在您的Details 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);

当然,通过调用myAdapter.notifyDataSetChanged();

,通知您的ListView数据已被更改

答案 1 :(得分:0)

你的PatientDetailsAdapter课程当然是一个内部阶级。顶级课程不能私密。如果您将关键字private应用于顶级类,则会出现编译错误,而不仅仅是警告。

由于您没有提供包含PatientDetailsAdapter的整个类,我只能猜测问题是您没有在顶级类或任何其他内部或嵌套类中明确使用您的类在顶级课程内。这肯定会引发警告。 (请注意,private范围不限制从顶级类内部访问您的类,即使是来自其他内部/嵌套类。“源文件”中的内容可以看到您的private类/字段/方法......)

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