我实现了自定义微调器:
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值?
答案 0 :(得分:5)
Object selection = sp.getSelectedItem();
这将返回您可以从
查询信息的所选项目 在您的适配器中,您需要实现getItem(int position)
并返回所需的项目,选择将为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
答案 1 :(得分:0)
我找到了一个适用于我的解决方案,onItemSelectedListener看起来像这样:
spnrCategories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
String item = ((TextView)view.findViewById(R.id.spinnerTextViewName)).getText().toString();
Toast.makeText(MainActivity.this, item , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView adapterView) {
}
});
这是我自定义的微调器布局:
<ImageView
android:id="@+id/spinnerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/baseline_add_black_18dp"
android:layout_weight="0"
android:padding="10dp"/>
<TextView
android:id="@+id/spinnerTextViewName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/spinnerImageView"
android:padding="11dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Add category1234"
android:textColor="#000000"
android:layout_weight="1"
android:textSize="20dp" />
<TextView
android:id="@+id/spinnerTextViewNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="11dp"
android:text="(0)"
android:textColor="#000000"
android:textSize="20dp" />
在此站点找到了我的解决方案: https://www.zoftino.com/android-spinner-custom-adapter-&-layout