无法解析符号'customAdapter'

时间:2017-10-14 08:13:50

标签: android

我正在编写我的第一个Android应用程序,我无法解决以下问题。

我想制作一个自定义列表,但收到错误cannot resolve symbol 'customAdapter'

由于我不是专家,我无法导入或者我不知道,我只是一个初学者。

变量:

int[] IMAGES = {R.drawable.doctor_1, R.drawable.doctor_1, ...};
String[] NAMES = {"Doctor 1", "Doctor 2", "Doctor 3", ...};
String[] DESCRIPTION = {"MBBS", "MBBS", "MBBS", ...};
String[] TIME = {"4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", ...};
String[] FEES = {"RS 2000", "RS 500", ...};

这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android.app.ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);


    ListView listView = (ListView) findViewById(R.id.listview);

    CustomAdapter customAdapter= new CustomAdapter();
    listView.setAdapter(CustomAdapter);

    class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return IMAGES.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.customlayout,null);
            ImageView imageview = (ImageView)view.findViewById(R.id.imageView);
            TextView textView_name=(TextView)view.findViewById(R.id.name);
            TextView textView_description= (TextView)view.findViewById(R.id.description);
            TextView textView_time= (TextView)view.findViewById(R.id.time);
            TextView textView_fees= (TextView)view.findViewById(R.id.fees);

            imageview.setImageResource(IMAGES[i]);
            textView_name.setText(NAMES[i]);
            textView_description.setText(DESCRIPTION[i]);
            textView_time.setText(TIME[i]);
            textView_fees.setText(FEES[i]);

            return view;
        }
    }

1 个答案:

答案 0 :(得分:2)

使您的CustomAdapter类超出onSavedInstanceState()方法范围: 改变像这样:

int[] IMAGES = {R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1,
        R.drawable.doctor_1, R.drawable.doctor_1, R.drawable.doctor_1};

String[] NAMES = {"Doctor 1", "Doctor 2", "Doctor 3", "Doctor 4", "Doctor 5", "Doctor 6", "Doctor 7", "Doctor 8", "Doctor 9",
        "Doctor 10"};


String[] DESCRIPTION = {"MBBS", "MBBS", "MBBS", "MBBS", "MBBS",
        "MBBS", "MBBS", "MBBS", "MBBS", "MBBS"};

String[] TIME = {"4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm", "4:00pm-10:00pm",
        "4:00pm-10:00pm", "4:00pm-10:00pm"};

String[] FEES = {"RS 2000", "RS 500", "RS 200", "RS 2000", "RS 500", "RS 200", "RS 2000", "RS 500", "RS 200", "RS 1000"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android.app.ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);


    ListView listView = (ListView) findViewById(R.id.listview);

    CustomAdapter customAdapter= new CustomAdapter();
    listView.setAdapter(CustomAdapter);


    }

 class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return IMAGES.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.customlayout,null);
            ImageView imageview = (ImageView)view.findViewById(R.id.imageView);
            TextView textView_name=(TextView)view.findViewById(R.id.name);
            TextView textView_description= (TextView)view.findViewById(R.id.description);
            TextView textView_time= (TextView)view.findViewById(R.id.time);
            TextView textView_fees= (TextView)view.findViewById(R.id.fees);

            imageview.setImageResource(IMAGES[i]);
            textView_name.setText(NAMES[i]);
            textView_description.setText(DESCRIPTION[i]);
            textView_time.setText(TIME[i]);
            textView_fees.setText(FEES[i]);

            return view;
        }