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
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 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;
}
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) {
}
});
Populate spinner [custom adapter] retrofit
Ok, so to implement this you need first of all to create you xml view, for example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Now in adapter in function getView()
you need to inflate it and bind the TextView
:
convertView = LayoutInflater.from(context).
inflate(R.layout.layout_list_view_row_items, parent, false);
TextView nameTextView = (TextView) convertView.findViewById(R.id.name_text_view);
//and now get the SpinnerArrayObject by position
SpinnerArrayObject currentObject = spinnerArrayObjects.get(position);
nameTextView.setText(currentObject.getName());
return convertView;
How could i add a spinner in listview with its listitems by using customadapter?
Here is the code.
package com.android.main;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
public class DemoListSpinnerActivity extends Activity {
ListView _listview;
String[] itemsarray=new String[]{"one","two","three"};
ArrayAdapter<String> adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsarray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
_listview=(ListView)findViewById(R.id.listView1);
_listview.setAdapter(new CustomAdapter(this));
}
private class CustomAdapter extends BaseAdapter
{
LayoutInflater inflater;
public CustomAdapter(Context context)
{
inflater=LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int arg0, View convertview, ViewGroup arg2) {
ViewHolder viewHolder;
if(convertview==null)
{
convertview=inflater.inflate(R.layout.listrow,null);
viewHolder=new ViewHolder();
viewHolder.spinner=(Spinner)convertview.findViewById(R.id.spinner1);
viewHolder.spinner.setAdapter(adapter);
convertview.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)convertview.getTag();
}
return convertview;
}
public class ViewHolder
{
Spinner spinner;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_width="match_parent"></ListView>
</LinearLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/spinner1"></Spinner>
</LinearLayout>
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>
Related Topics
Install an APK File from Command Prompt
Does Android Keep the .Apk Files? If So Where
Difference Between Activity and Fragmentactivity
How to Change the Textcolor on an Android Searchview
How to Find If a Particular Package Exists on My Android Device
How to Know When an Edittext Loses Focus
What Is the Maximum Amount of Ram an App Can Use
Android Eclipse Ddms - Can't Access Data/Data/ on Phone to Pull Files
Get Center Visible Item of Recycleview When Scrolling
Xamarin.Forms Listview: Set the Highlight Color of a Tapped Item
Monodroid: Error When Calling Constructor of Custom View - Twodscrollview
Changing Background Color of Selected Item in Recyclerview
Install_Failed_Duplicate_Permission... C2D_Message
Edittext, Clear Focus on Touch Outside
Actionbar Up Navigation with Fragments