是否可以从类的ArrayList的特定字段填充微调器?

问题描述 投票:0回答:1

我有这个类的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个回答
1
投票

看看SpinnerAdaper。

如果你有一个带有Lawyer类的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;
    }


}
© www.soinside.com 2019 - 2024. All rights reserved.