0

我有这个类的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
    }
});
4

1 回答 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;
    }


}
于 2013-11-12T10:54:00.820 回答