gpt4 book ai didi

android - 获取定制旋转器项目

转载 作者:行者123 更新时间:2023-12-01 16:49:01 31 4
gpt4 key购买 nike

我实现了自定义微调器:

 public class MyAdapter extends ArrayAdapter<String>
{

public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}

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

public View getCustomView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater=getLayoutInflater();
View row= inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(data1[position]);

ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
icon.setImageResource(images[position]);

return row;
}

}

并设置为这样的微调器:

spinner_choose_network_spinner.setAdapter(new MyAdapter(this, R.layout.spinner, data1));

我的问题是如何获取微调器当前所选项目的 textview 值?

最佳答案

Object selection = sp.getSelectedItem();

这将返回您可以从中查询信息的所选项目

在您的适配器中,您需要实现getItem(intposition)

并返回所需的项目,选择将是getItem(selectedPosition)

假设你的 getItem 是

public String getItem(int position){
return data1[position];

}

然后String selected = spinner.getSelectedItem();

将返回data1[selectedPosition]

为了简单起见,我建议您创建一个新对象

public class MyCustomObject{

String title;
int imageResource;

}

然后你的数据数组将是

ArrayList<MyCustomObject>data = new ArrayList<MyCustomObject>();

以某种方式填充这个数组

现在你的行将是

        public View getCustomView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater=getLayoutInflater();
View row= inflater.inflate(R.layout.spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.textView1);
label.setText(data.get(position).title);

ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
icon.setImageResource(data.get(position).imageResource);

return row;
}

然后你的 getItem 将是

public MyCustomObject getItem(int position){

return data.get(position);
}

然后您的选择将是

MyCustomObject selection = spinner.getSelectedItem();

您可以看到 selection.title 以及 selection.imageResource 如果您需要的话将可用

关于android - 获取定制旋转器项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23348875/

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