我被困在一个Android Spinner上几个小时,我无法找到我的方式做错了。
我已经设置了onItemSelectedListener方法,但它在我的活动启动时触发,并且单击列表中的项目的事实不会改变任何内容。这是代码:
Spinner的自定义适配器:
public class spinnerUrgenceAdapter extends ArrayAdapter {
ArrayList<UrgenceSpin> list;
Context ctx;
public spinnerUrgenceAdapter(Context context, ArrayList<UrgenceSpin> list) {
super(context, 0, list);
this.list = list;
this.ctx=context;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return customView(position, convertView, parent);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return customView(position, convertView, parent);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public View customView(int position, View convertView, ViewGroup parent){
UrgenceSpin us = (UrgenceSpin) getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_spinner, parent, false);
}
Button cercle = (Button) convertView.findViewById(R.id.urgenceDSpin);
TextView nom = (TextView) convertView.findViewById(R.id.urgenceLSpin);
cercle.setBackground(ctx.getResources().getDrawable(us.getIdDrawable()));
nom.setText(us.getNomUrgence());
return convertView;
}
我创建它并实现监听器的方式:
final int[] urgencesDr = {R.drawable.no_urgence, R.drawable.faible_urgence, R.drawable.moyenne_urgence, R.drawable.pressant_urgence, R.drawable.urgent_urgence};
final String[] urgencesLa = {"Pas d'urgence", "Urgence très faible" , "Peu urgent", "Pressant", "Urgent"};
ArrayList<UrgenceSpin> urgsp = new ArrayList<>();
for(int i=0; i<5; i++){
urgsp.add(new UrgenceSpin(urgencesDr[i], urgencesLa[i]));
}
Spinner spin = (Spinner) findViewById(R.id.urgencespin);
spinnerUrgenceAdapter sua = new spinnerUrgenceAdapter(CreationSav.this, urgsp);
spin.setAdapter(sua);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Button urgenceD = (Button) view.findViewById(R.id.urgenceDSpin);
TextView urgenceL = (TextView) view.findViewById(R.id.urgenceLSpin);
Toast.makeText(CreationSav.this, " yolo "+position, Toast.LENGTH_LONG).show();
/*
UrgenceSpin current = new UrgenceSpin(urgencesDr[position], urgencesLa[position]);
urgenceD.setBackground(CreationSav.this.getResources().getDrawable(current.getIdDrawable()));
urgenceL.setText(current.getNomUrgence());
*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
您可以注意到,我的最终目标是更改按钮和textView(最后的屏幕截图) 和适配器的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<Button
android:layout_gravity="center_vertical"
android:layout_width="13dp"
android:layout_height="13dp"
android:layout_marginRight="8dp"
android:id="@+id/urgenceDSpin"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:id="@+id/urgenceLSpin"/>
</LinearLayout>
</LinearLayout>
因为您可以注意到屏幕截图,当我点击我的微调器时会出现Row,但是单击其中一个项目不会执行任何操作。当我的活动开始时,我得到了我的祝酒词:Yolo 0
我的方式出了什么问题,如果我做得对,你能解释一下吗?我只是试着遵循基础,但它只是不起作用......而且,是否可以做我正在尝试做的事情(用所选元素替换微调器中的当前元素)
答案 0 :(得分:0)
试试这个
int selectionCurrent = spinner.getSelectedItemPosition();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (selectionCurrent != i) {
}
selectionCurrent = i;
}