Difference between getView & getDropDownView in SpinnerAdapter

Win Myo Htet picture Win Myo Htet · Nov 17, 2012 · Viewed 31.2k times · Source

When you implement SpinnerAdapter you get getDropDownView, how does it differ from getView which you have when you need to extend BaseAdapter.

Answer

Win Myo Htet picture Win Myo Htet · Nov 17, 2012

If we look at the following code, we have name and value array in getView and getDropDownView.

private void initView() {
    SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sddadapter.name);

    Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
    getViewSP.setAdapter(adapter);

    Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
    getViewWDropDownSP.setAdapter(sddadapter);
}

static class SpinnerDropDownAdapter extends BaseAdapter implements
        SpinnerAdapter {
    Context context;

    SpinnerDropDownAdapter(Context ctx) {
        context = ctx;
    }

    String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
            " Seven", " Eight" };
    String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };

    @Override
    public int getCount() {
        return name.length;
    }

    @Override
    public String getItem(int pos) {
        // TODO Auto-generated method stub
        return name[pos];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(name[position]);
        return text;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(value[position]);
        return text;
    }
}

If getDropDownView method is not implemented, the drop down pop up will get the view from getView. Thus, it will only show name.

getView

When both getView and getDropDownView is implemented, the former getting name and the latter getting value, the spinner at rest will get name from getview and the drop down pop up will get value. getView and getDropDownView