【问题标题】:Is it possible to have a spinner filled from specific field of an ArrayList of a class?是否可以从类的 ArrayList 的特定字段填充微调器?
【发布时间】:2019-03-08 13:31:20
【问题描述】:

我有一个此类的arrayList:

public class Lawyer extends DbHelper{
    private int id;
    private String fullName;
    private String mobile;

    private Context context;

    public Lawyer(Context context,int id, String fullName, String mobile) {
        super(context);
        this.id = id;
        this.fullName = fullName;
        this.mobile = mobile;

        this.context = context;     
    }
}

我的arrayList 充满了几个人的信息。是否可以在数组列表中填充所有律师的“fullName”? 目前我有这段代码可以用一个简单数组中的静态数据填充我的微调器:

String [] items={"Lawyer1","Lawyer2","Lawyer3","Lawyer4","Lawyer5"};
final Spinner lstLawyers;

lstLawyers=(Spinner)findViewById(R.id.lstLawyers);
lstLawyers.setAdapter( new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item,items));
lstLawyers.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        EditText txtMessage = (EditText) findViewById(R.id.txtMessage);
        txtMessage.setText(lstLawyers.getSelectedItem().toString());

    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
});

【问题讨论】:

    标签: android data-binding arraylist spinner


    【解决方案1】:

    看看 SpinnerAdaper。

    如果您有一个带有律师类的 alTypes ArrayList(带有 fullName、mobile 等...) 您应该扩展 SpinnerAdapter(请参阅下面的 AdapterForSpinner1)

    然后将其设置为您的微调器,如下所示:

    AdapterForSpinner1 spinad1 = new AdapterForSpinner1(activity, alTypes);
    
                 lstLawyers.setAdapter(spinad1);
    

    AdapterForSpinner1

    class AdapterForSpinner1 extends BaseAdapter implements SpinnerAdapter {
    
        /**
         * The internal data (the ArrayList with the Objects).
         */
        private final List<Lawyer> data;
        Context mContext;
    
        public AdapterForSpinner1(Context context, List<Lawyer> data){
            this.data = data;
            mContext=context;
        }
    
        /**
         * Returns the Size of the ArrayList
         */
        @Override
        public int getCount() {
            return data.size();
        }
    
        /**
         * Returns one Element of the ArrayList
         * at the specified position.
         */
        @Override
        public Object getItem(int position) {
            return data.get(position);
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
        /**
         * Returns the View that is shown when a element was
         * selected.
         */
        @Override
        public View getView(int position, View recycle, ViewGroup parent) {
            TextView text;
            if (recycle != null){
                // Re-use the recycled view here!
                text = (TextView) recycle;
            } else {
                // No recycled view, inflate the "original" from the platform:
                 LayoutInflater li = (LayoutInflater)  mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                text = (TextView) (li.inflate(android.R.layout.simple_dropdown_item_1line, parent, false)          );
            }
            text.setTextColor(Color.BLACK);
            text.setText(data.get(position).getFullname());
            return text;
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多