Difference between getView & getDropDownView in SpinnerAdapter

AndroidAndroid ArrayadapterAndroid SpinnerBaseadapter

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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;
    }
}

Solution 4 - Android

We can use SpinnerAdapter as this:

public class FolderSpinnerAdpater extends BaseAdapter implements SpinnerAdapter {
@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return null;
}
}

In BaseAdapter.java

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return getView(position, convertView, parent);
}

Override getView() and getDropDownView(),return different view,you will find the difference.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWin Myo HtetView Question on Stackoverflow
Solution 1 - AndroidWin Myo HtetView Answer on Stackoverflow
Solution 2 - AndroidLou MordaView Answer on Stackoverflow
Solution 3 - AndroidwannikView Answer on Stackoverflow
Solution 4 - AndroidAdoView Answer on Stackoverflow