Android spinner showing object reference instead of string

后端 未结 4 1426
耶瑟儿~
耶瑟儿~ 2021-01-04 01:52

Ok so I am having a issue with my spinner. Its being populated with data pulled from a webservice. The issue im having is that when the spinner is not clicked instead of sho

4条回答
  •  隐瞒了意图╮
    2021-01-04 02:22

    Ok so i solved this by doing a little more research and looking at the answer to this question Example of custom setDropDownViewResource spinner item

    I extracted what i was doing in getDropDownView and called it in both getView and getDropDownView.

    here is the code

    package com.android.main;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    public class DrinkAdapter extends ArrayAdapter
    {
        private Activity context;
        ArrayList data = null;
    
        public DrinkAdapter(Activity context, int resource, ArrayList data)
        {
            super(context, resource, data);
            this.context = context;
            this.data = data;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {   // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
    
            return initView(position, convertView, parent);  
        }
    
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent)
        {   // This view starts when we click the spinner.
            return initView(position, convertView, parent);
        }
    
        private View initView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if(row == null)
            {
                LayoutInflater inflater = context.getLayoutInflater();
                row = inflater.inflate(R.layout.dropdown_value_id, parent, false);
            }
    
            Drink item = data.get(position);
            String test = item.getName();
            Log.d("test ", test);
    
            if(item != null)
            {   
    
                TextView drinkName = (TextView) row.findViewById(R.id.item_value);
    
                if(drinkName != null){
                 drinkName.setText(item.getName());
                 Log.d("find me ", drinkName.toString());
                }
    
            }
    
            return row;
        }
    }
    

提交回复
热议问题