2017-10-14 21 views
-3

我正在編寫我的第一個android應用程序,我無法解決以下問題。無法解析符號'customAdapter'

我想製作自定義列表,但出現錯誤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

讓您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; 
     }