gpt4 book ai didi

Android:如何将微调器绑定(bind)到自定义对象列表?

转载 作者:IT老高 更新时间:2023-10-28 13:01:23 28 4
gpt4 key购买 nike

在用户界面中必须有一个微调器,其中包含一些名称(名称可见)并且每个名称都有自己的 ID(ID 不等于显示顺序)。当用户从列表中选择名称时,必须更改变量 currentID。

应用程序包含 ArrayList

其中 User 是具有 ID 和名称的对象:

public class User{
public int ID;
public String name;
}

What I don't know is how to create a spinner which displays the list of user's names and bind spinner items to IDs so when the spinner item is selected/changed the variable currentID is set to appropriate value.

如果有人能展示所描述问题的解决方案或提供任何有用的链接来解决问题,我将不胜感激。

谢谢!

最佳答案

我知道线程很旧,但以防万一......

用户对象:

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

以及实现:

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

关于Android:如何将微调器绑定(bind)到自定义对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1625249/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com