“ArrayAdapter requires the resource ID to be a TextView” xml problems

前端 未结 3 1345
生来不讨喜
生来不讨喜 2020-11-22 01:45

I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:25

    If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:

        //Pass in the resource id:  R.id.text_view
        SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
                R.id.text_view,
                new ArrayList<>());
    

    Adapter:

    public class SpinnerAdapter extends ArrayAdapter {
    
        private Context context;
        private List values;
    
        public SpinnerAdapter(Context context, int textViewResourceId,
                              List values) {
    
            //Pass in the resource id:  R.id.text_view
            super(context, textViewResourceId, values);
    
            this.context = context;
            this.values = values;
        }
    

提交回复
热议问题