How to create Spinner-list using CustomAdapter in android
Inside your adapter constructor, send the id of textview also
public CustomAdapter(Activity context,int resouceId, int textviewId, List<RowItem> list){
super(context,resouceId,textviewId, list);
flater = context.getLayoutInflater();
}
call it by
CustomAdapter adapter = new CustomAdapter(MainActivity.this,
R.layout.listitems_layout, R.id.title, rowItems);
EDIT
Your images are not showing because you didn't override the getDropdownView() method. This method decides the layout of child when dropdown is visible. So add this method to your adapter
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = flater.inflate(R.layout.list_itemslayout,parent, false);
}
RowItem rowItem = getItem(position);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem.getTitle());
ImageView imageView = (ImageView) convertView.findViewById(R.id.icon);
imageView.setImageResource(rowItem.getImageId());
return convertView;
}
Suggestion
in your getView() put a check for (convertview == null). It might not have any impact on this small adapter but in case your adapter is having more items, it will impact the performance.
EDIT
To put your spinner dropdown below the anchor, use
android:overlapAnchor="false"
inside your spinner
Custom Spinner Adapter
Try
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(yourRowlayout, parent,
false);
TextView make = (TextView) row.findViewById(R.id.Make);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(yourRowlayout, parent,
false);
TextView make = (TextView) row.findViewById(R.id.Make);
Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
"fonts/gilsanslight.otf");
v.setTypeface(myTypeFace);
v.setText(itemList.get(position));
return row;
}
Android: How to bind spinner to custom object list?
You can look at this answer. You can also go with a custom adapter, but the solution below is fine for simple cases.
Here's a re-post:
So if you came here because you want to have both labels and values in the Spinner - here's how I did it:
- Just create your
Spinner
the usual way - Define 2 equal size arrays in your
array.xml
file -- one array for labels, one array for values - Set your
Spinner
withandroid:entries="@array/labels"
When you need a value, do something like this (no, you don't have to chain it):
String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];
Custom ArrayAdapter for Spinner getView() not called
Problem solved. First mistake was that I had two views with same id and I set my adapter to the one which not visible.
Second was that I had some mistakes in code of my custom adapter. I post it below.
public class DishesFilterCustomArrayAdapter extends ArrayAdapter<Dish> {
private List<Dish> items;
private Context context;
public DishesFilterCustomArrayAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Dish> objects) {
super(context, resource, textViewResourceId, objects);
this.items = objects;
this.context = context;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.receipt_history_spinner_item, null);
}
TextView lbl = (TextView) v.findViewById(R.id.receiptHistorySpinnerItemTextView);
lbl.setTextColor(context.getResources().getColor(R.color.blue_light));
lbl.setText(items.get(position).dishName);
return v;
}
@Override
public Dish getItem(int position) {
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(context);
v = inflater.inflate(R.layout.receipt_history_spinner_item, null);
}
TextView lbl = (TextView) v.findViewById(R.id.receiptHistorySpinnerItemTextView);
lbl.setTextColor(context.getResources().getColor(R.color.blue_light));
lbl.setText(items.get(position).dishName);
return v;
}
@Override
public int getCount() {
return items.size();
}
public List<Dish> getItems() {
return items;
}
Mike.M thanks you for your help!
How to customize a Spinner in Android
Create a custom adapter with a custom layout for your spinner.
Spinner spinner = (Spinner) findViewById(R.id.pioedittxt5);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.travelreasons, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
R.layout.simple_spinner_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
R.layout.simple_spinner_dropdown_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="@style/spinnerDropDownItemStyle"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee" />
In styles add your custom dimensions and height as per your requirement.
<style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
</style>
<style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
</style>
Android custom spinner SelectedItem
You can get the selected spinner item's text in the following way:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
String selectedText=list.get(pos).getPlanCategorytext();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
OR
String selectedItemText=list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();
Where list is the arraylist with ItemData which you used above to populate the data in spinner.
Also about this:
String spinnerselection = categorySpinnerjava.getSelectedItem().toString();
You have to replace categorySpinnerjava.getSelectedItem().toString();
with list.get(spinner.getSelectedItemPosition()).getPlanCategorytext();
Set value for Spinner with custom Adapter in Android
@Haresh Chhelana example is good, However if you want to show both name and code in spinner after selecting, check this out.
List<Map<String, String>> items = new ArrayList<Map<String, String>>();
for (int i = 0; i < JA.length(); i++) {
json = JA.getJSONObject(i);
mapData = new HashMap<String, String>();
mapData.put("name", json.getString("Name"));
mapData.put("code", json.getString("Code"));
items.add(mapData);
}
SimpleAdapter adapter = new SimpleAdapter(this, items, android.R.layout.simple_list_item_2, new String[] {
"name", "code" }, new int[] { android.R.id.text1, android.R.id.text2 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
And Spinner selected item callback
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Map<String, String> selectedItem = (Map<String, String>) parent.getSelectedItem();
String name=selectedItem.get("name");
String code=selectedItem.get("code");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related Topics
Load Images from Disk Cache with Picasso If Offline
Android: Google Maps Location with Low Battery Usage
How to Search for a Value in Firebase Android
How to Store Large Blobs in an Android Content Provider
Capturing Images with Mediastore.Action_Image_Capture Intent in Android
Communicating with Serial Usb Device Over Android
Android: Broadcastreceiver Intent to Detect Camera Photo Taken
How to Set Up Multiple Listeners for One Event
How to Detect When the User Launches Another App? (Android)
How to Get Chrome Browsing History/Bookmarks in Our Android App
In Espresso, How to Avoid Ambiguousviewmatcherexception When Multiple Views Match
Kitkat Kills: Not Allowed to Load Local Resource: File:///Android_Asset/Webkit/Android-Weberror.Png
Launching an Android Application from the Browser
Saving Arraylists in SQLite Databases
How to Use Default Android Drawables
How to Have 1 Firebase Database for 2 Apps with Different Package Names