在用户界面中,必须有它包含一些名称(名称是可见的),并且每个名字具有其自己的ID的纺丝器(ID不等于显示序列)。 当用户从列表中选择的名称的变量currentID必须被改变。
该应用程序包含的ArrayList
当用户与ID和名称的对象:
public class User{
public int ID;
public String name;
}
我不知道的是如何创建一个显示用户名列表中选择一个微调和微调绑定物品,所以当选择了微调项ID /改变变量currentID设定为合适的值。
我将不胜感激,如果有人能证明所描述的问题的解决或提供解决问题的有用的任何链接。
谢谢!
Answer 1:
你可以看一下这个答案 。 你也可以去使用自定义适配器,但下面的解决方案是对简单的场合。
这里有一个重职:
所以,如果你来到这里,因为你想在微调的标签,以及价值观 - 这里是我做到了:
- 您只需建立
Spinner
的常用方法 - 限定在2个相等大小的数组
array.xml
文件-一个阵列标签,一个数组值 - 设置您的
Spinner
与android:entries="@array/labels"
当你需要的值,做这样的事情(不,你不必链吧):
String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];
Answer 2:
我知道线程是旧的,但以防万一...
用户对象:
public class User{
private int _id;
private String _name;
public User(){
this._id = 0;
this._name = "";
}
public void setId(int id){
this._id = id;
}
public int getId(){
return this._id;
}
public void setName(String name){
this._name = name;
}
public String getName(){
return this._name;
}
}
定制微调适配器(ArrayAdapter)
public class SpinAdapter extends ArrayAdapter<User>{
// Your sent context
private Context context;
// Your custom values for the spinner (User)
private User[] values;
public SpinAdapter(Context context, int textViewResourceId,
User[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
@Override
public int getCount(){
return values.length;
}
@Override
public User getItem(int position){
return values[position];
}
@Override
public long getItemId(int position){
return position;
}
// And the "magic" goes here
// This is for the "passive" state of the spinner
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I created a dynamic TextView here, but you can reference your own custom layout for each spinner item
TextView label = (TextView) super.getView(position, convertView, parent);
label.setTextColor(Color.BLACK);
// Then you can get the current item using the values array (Users array) and the current position
// You can NOW reference each method you has created in your bean object (User class)
label.setText(values[position].getName());
// And finally return your dynamic (or custom) view for each spinner item
return label;
}
// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = (TextView) super.getDropDownView(position, convertView, parent);
label.setTextColor(Color.BLACK);
label.setText(values[position].getName());
return label;
}
}
而implementarion:
public class Main extends Activity {
// You spinner view
private Spinner mySpinner;
// Custom Spinner adapter (ArrayAdapter<User>)
// You can define as a private to use it in the all class
// This is the object that is going to do the "magic"
private SpinAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the Users array
// You can get this retrieving from an external source
User[] users = new User[2];
users[0] = new User();
users[0].setId(1);
users[0].setName("Joaquin");
users[1] = new User();
users[1].setId(2);
users[1].setName("Alberto");
// Initialize the adapter sending the current context
// Send the simple_spinner_item layout
// And finally send the Users array (Your data)
adapter = new SpinAdapter(Main.this,
android.R.layout.simple_spinner_item,
users);
mySpinner = (Spinner) findViewById(R.id.miSpinner);
mySpinner.setAdapter(adapter); // Set the custom adapter to the spinner
// You can create an anonymous listener to handle the event when is selected an spinner item
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// Here you get the current item (a User object) that is selected by its position
User user = adapter.getItem(position);
// Here you can do the action you want to...
Toast.makeText(Main.this, "ID: " + user.getId() + "\nName: " + user.getName(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapter) { }
});
}
}
Answer 3:
简单的解决方案
在SO精练不同的解决方案之后,我发现下面是用于填充一个最简单和干净的解决方案Spinner
自定义Objects
。 下面是完整的实现:
User.java
public class User{
public int ID;
public String name;
@Override
public String toString() {
return this.name; // What to display in the Spinner list.
}
}
RES /布局/ spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="14sp"
android:textColor="#FFFFFF"
android:spinnerMode="dialog" />
RES /布局/ your_activity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/user" />
</LinearLayout>
您的活动
// Gets all users but replace with whatever list of users you want.
List<User> users = User.all();
ArrayAdapter userAdapter = new ArrayAdapter(this, R.layout.spinner, users);
Spinner userSpinner = (Spinner) findViewById(R.id.user);
userSpinner.setAdapter(userAdapter);
// And to get the actual User object that was selected, you can do this.
User user = (User) ( (Spinner) findViewById(R.id.user) ).getSelectedItem();
Answer 4:
对于简单的解决方案,你可以只覆盖了“的toString”在你的对象
public class User{
public int ID;
public String name;
@Override
public String toString() {
return name;
}
}
然后你可以使用:
ArrayAdapter<User> dataAdapter = new ArrayAdapter<User>(mContext, android.R.layout.simple_spinner_item, listOfUsers);
这样,你的微调将只显示用户名。
Answer 5:
只是一个小的调整,以华阿尔贝托的回答可以解决的风格issue.Just更换getDropDownView功能的自定义适配器如下,
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
TextView tv = ((TextView) v);
tv.setText(values[position].getName());
tv.setTextColor(Color.BLACK);
return v;
}
Answer 6:
对我来说工作得很好,周围的getResource()的东西所需要的代码如下:
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> spinner, View v,
int arg2, long arg3) {
String selectedVal = getResources().getStringArray(R.array.compass_rate_values)[spinner.getSelectedItemPosition()];
//Do something with the value
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
只需要确保(自己)在两个数组中的值正确对齐!
Answer 7:
由华金·阿尔贝托的启发,这个工作对我来说:
public class SpinAdapter extends ArrayAdapter<User>{
public SpinAdapter(Context context, int textViewResourceId,
User[] values) {
super(context, textViewResourceId, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = (TextView) super.getView(position, convertView, parent);
label.setTextColor(Color.BLACK);
label.setText(this.getItem(position).getName());
return label;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
TextView label = (TextView) super.getView(position, convertView, parent);
label.setTextColor(Color.BLACK);
label.setText(this.getItem(position).getName());
return label;
}
}
Answer 8:
基于华阿尔贝托(感谢)样本,但它适用于任何类型的(你应该实现在类型的toString(),这样你就可以格式化输出。
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class SpinAdapter<T> extends ArrayAdapter<T> {
private Context context;
private List<T> values;
public SpinAdapter(Context context, int textViewResourceId, List<T> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount() {
return values.size();
}
public T getItem(int position) {
return values.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setText(values.toArray(new Object[values.size()])[position]
.toString());
return label;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setText(values.toArray(new Object[values.size()])[position]
.toString());
return label;
}
}
此外我认为你可以通过替换数组列表,这样你就不需要做指定者在列表中,但我有一个列表..... :)
Answer 9:
到目前为止,我已经找到了最简单的方法:
@Override
public String toString() {
return this.label;
}
现在,你可以在你的微调坚持任何对象,它将显示指定的标签。
Answer 10:
为了理解的伎俩,一个人必须知道,适配器一般和ArrayAdapter如何工作尤其如此。
适配器:是能够对数据结构结合到窗口小部件,则这些部件被显示在列表中或在该微调数据对象。
所以,这两个问题的答案适配器是:
- 其窗口小部件或复合视图需要与一定索引的数据结构(类对象)相关联的?
- 如何提取从数据结构即数据(你的类的对象)以及如何设置字段(一个或多个)
EditText
微件的或复合视图根据该数据?
ArrayAdapter的答案是:
- 每个窗口小部件(即
row.xml
OR android.R.layout.simple_spinner_item
任何索引)是相同的,并且从它的ID是给ArrayAdapter的构造函数的资源膨胀。 - 每个插件预计TextView的(或子孙)的一个实例。 小部件的
.setText()
方法将与该项目在支撑数据结构中的字符串格式被使用。 字符串格式将通过调用来获得.toString()
上的项目。
CustomListViewDemo.java
public class CustomListViewDemo extends ListActivity {
private EfficientAdapter adap;
private static String[] data = new String[] { "0", "1", "2", "3", "4" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
adap = new EfficientAdapter(this);
setListAdapter(adap);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
}
public static class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Context context;
int firstpos=0;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adaptor_content, null);
holder = new ViewHolder();
holder.sp = (Spinner) convertView.findViewById(R.id.spinner1);
holder.ArrayAdapter_sp = new ArrayAdapter(parent.getContext(),android.R.layout.simple_spinner_item,data);
holder.ArrayAdapter_sp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.sp.setAdapter( holder.ArrayAdapter_sp);
holder.sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
private int pos = position;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int p, long arg3)
{
// TODO Auto-generated method stub
Toast.makeText(context, "select spinner " + String.valueOf(pos)+" with value ID "+p, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder
{
Spinner sp;
ArrayAdapter ArrayAdapter_sp;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
}
}
adaptor_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="314dp"
android:layout_height="wrap_content" />
</LinearLayout>
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="60dip"
android:layout_marginTop="10dip"
android:cacheColorHint="#00000000"
android:drawSelectorOnTop="false" />
</RelativeLayout>
它正常工作,我希望它是有用的。
Answer 11:
我的自定义对象
/**
* Created by abhinav-rathore on 08-05-2015.
*/
public class CategoryTypeResponse {
private String message;
private int status;
private Object[] object;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Object[] getObject() {
return object;
}
public void setObject(Object[] object) {
this.object = object;
}
@Override
public String toString() {
return "ClassPojo [message = " + message + ", status = " + status + ", object = " + object + "]";
}
public static class Object {
private String name;
private String _id;
private String title;
private String desc;
private String xhdpi;
private String hdpi;
private String mdpi;
private String hint;
private String type;
private Brands[] brands;
public String getId() {
return _id;
}
public void setId(String id) {
this._id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getXhdpi() {
return xhdpi;
}
public void setXhdpi(String xhdpi) {
this.xhdpi = xhdpi;
}
public String getHdpi() {
return hdpi;
}
public void setHdpi(String hdpi) {
this.hdpi = hdpi;
}
public String getMdpi() {
return mdpi;
}
public void setMdpi(String mdpi) {
this.mdpi = mdpi;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getHint() {
return hint;
}
public void setHint(String hint) {
this.hint = hint;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Brands[] getBrands() {
return brands;
}
public void setBrands(Brands[] brands) {
this.brands = brands;
}
@Override
public String toString() {
return "ClassPojo [name = " + name + "]";
}
}
public static class Brands {
private String _id;
private String name;
private String value;
private String categoryid_ref;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getCategoryid_ref() {
return categoryid_ref;
}
public void setCategoryid_ref(String categoryid_ref) {
this.categoryid_ref = categoryid_ref;
}
@Override
public String toString() {
return name;
}
}
}
我也想设置这个对象作为我的适配器源到我的微调,而不ArrayAdapter延伸,使我所做的。
brandArray = mCategoryTypeResponse.getObject()[fragPosition].getBrands();
ArrayAdapter brandAdapter = new ArrayAdapter< CategoryTypeResponse.Brands>(getActivity(),
R.layout.item_spinner, brandArray);
现在,你将能够看到你的微调结果,诀窍是要重写toString()
在你的自定义对象 ,所以什么都看重你想在微调显示刚刚返回,在此方法。
Answer 12:
我认为,最好的解决办法是“简单的解决方案”由乔什-品特 。
这为我工作:
//Code of the activity
//get linearLayout
LinearLayout linearLayout = (LinearLayout ) view.findViewById(R.id.linearLayoutFragment);
LinearLayout linearLayout = new LinearLayout(getActivity());
//display css
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//create the spinner in a fragment activiy
Spinner spn = new Spinner(getActivity());
// create the adapter.
ArrayAdapter<ValorLista> spinner_adapter = new ArrayAdapter<ValorLista>(getActivity(), android.R.layout.simple_spinner_item, meta.getValorlistaList());
spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn.setAdapter(spinner_adapter);
//set the default according to value
//spn.setSelection(spinnerPosition);
linearLayout.addView(spn, params2);
//Code of the class ValorLista
import java.io.Serializable;
import java.util.List;
public class ValorLista implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4930195743192929192L;
private int id;
private String valor;
private List<Metadato> metadatoList;
public ValorLista() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getValor() {
return valor;
}
public void setValor(String valor) {
this.valor = valor;
}
public List<Metadato> getMetadatoList() {
return metadatoList;
}
public void setMetadatoList(List<Metadato> metadatoList) {
this.metadatoList = metadatoList;
}
@Override
public String toString() {
return getValor();
}
}
Answer 13:
做:
spinner.adapter = object: ArrayAdapter<Project>(
container.context,
android.R.layout.simple_spinner_dropdown_item,
state.projects
) {
override fun getDropDownView(
position: Int,
convertView: View?,
parent: ViewGroup
): View {
val label = super.getView(position, convertView, parent) as TextView
label.text = getItem(position)?.title
return label
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val label = super.getView(position, convertView, parent) as TextView
label.text = getItem(position)?.title
return label
}
}
文章来源: Android: How to bind spinner to custom object list?