Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between getView & getDropDownView in SpinnerAdapter

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

like image 517
Win Myo Htet Avatar asked Nov 17 '12 19:11

Win Myo Htet


3 Answers

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

like image 60
Win Myo Htet Avatar answered Nov 02 '22 08:11

Win Myo Htet


The accepted answer seems a little complicated, so for simplicity

  • getView - the value shown on the spinner before the user presses the spinner, where each value view can be adjusted with the convertView parameter

  • getDropDownView - list of values the user can select after the user presses the spinner, where each value in the list can be adapted with the convertView parameter

like image 57
Lou Morda Avatar answered Nov 02 '22 07:11

Lou Morda


getView and getDropDownView can show different layout.

I create a Spinner and use class ImageTextAdapter extends ArrayAdapter<String> as an adapter. I override getView so that it can show both ImageView and TextView. However, I don't override getDropDownView.

This is a screenshot before the spinner is pressed

enter image description here

and after the spinner is pressed.

enter image description here

I initialize the spinner with the following code.

String[] spinnerTexts = {"Tanzania", "Thailand"};
int[] spinnerImages = {R.drawable.tanzania, R.drawable.thailand};

ArrayAdapter<String> adapter = new ImageTextAdapter(this, spinnerTexts, spinnerImages);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);

Here is ImageTextAdapter.java

public class ImageTextAdapter extends ArrayAdapter<String> {

    int[] images;

    public ImageTextAdapter(Context ctx, String[] texts, int[] images) {
        super(ctx, android.R.layout.simple_spinner_item, texts);
        this.images = images;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = getLayoutInflater().inflate(R.layout.row, parent, false);

        TextView textView = (TextView) row.findViewById(R.id.textView);
        textView.setText(getItem(position));

        ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
        imageView.setImageResource(images[position]);

        return row;
    }
}
like image 7
wannik Avatar answered Nov 02 '22 06:11

wannik