自定义微调器(获得不同的值)

时间:2017-01-09 18:00:05

标签: java android

我尝试创建自定义微调器并几乎成功。 Spinner在下拉场景中显示奇怪的值,但是当选择项目时,我得到正确的值...问题是为什么我会得到奇怪的值?I get the correct value during dropdown..weird values spinnerActivity.java

public class customSpinner extends AppCompatActivity {

Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_spinner);

    spinner=(Spinner)findViewById(R.id.spinner_id);

    ArrayList<spinner_info> arrayList1 = new ArrayList<>();


    arrayList1.add(new spinner_info("a"));
    arrayList1.add(new spinner_info("b"));
    arrayList1.add(new spinner_info("c"));
    arrayList1.add(new spinner_info("d"));

    spinner_adapter sAdapter = new spinner_adapter(this,arrayList1);
     sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(sAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

spinner_Adapter.java

 public class spinner_adapter extends ArrayAdapter<spinner_info> {

public spinner_adapter(Context context,ArrayList<spinner_info> arrayList1) {
    super(context,0,arrayList1);
}




@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    View viewItem = convertView;
    if(viewItem==null)
    {
        viewItem= LayoutInflater.from(getContext()).inflate(spinner_item,parent,false);
    }

    spinner_info currentinfo=getItem(position);

    TextView name = (TextView) viewItem.findViewById(R.id.item_spinner);
    name.setText(currentinfo.getSub());

    return viewItem;

}
}

spinner_Info.java

public class spinner_info{
String subject1;

public spinner_info(String subject){
    subject1=subject;
}

public String getSub()      {return subject1;}
}

spinner_Item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="TextView"
    android:textSize="18sp"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_spinner" />
</LinearLayout>  

1 个答案:

答案 0 :(得分:1)

ArrayAdapter基本上只调用它在数组中找到的值toString()。你正在使用一个自定义POJO(spinner_info),其中一个字段是一个字符串,所以它正在调用你的类并调用toString(),它为你提供了完全限定的类名和内存地址。

鉴于你的POJO只有String字段,你可以摆脱那个POJO并传递ArrayAdapter一个字符串列表,你的Spinner应该正确显示它们。然后,当选择一个时,您可以检查字符串的值。

<强>实施

ArrayList<String> arrayList1 = new ArrayList<>();

arrayList1.add("a");
arrayList1.add("b");
arrayList1.add("c");
arrayList1.add("d");

spinner_adapter sAdapter = new SpinnerAdapter(this, arrayList1);

spinner.setAdapter(sAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String selection = arrayList1.get(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

SpinnerAdapter类

public class SpinnerAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;

    public List<String> strings;

    public SpinnerAdapter(Context context, List<String> strings) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

        this.strings = strings;
    }

    @Override
    public int getCount() {
        return strings != null ? strings.size() : 0;
    }

    @Override
    public String getItem(int position) {
        return strings != null ? strings.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = inflater.inflate(R.layout.spinner_item, parent, false);
        }

        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = inflater.inflate(R.layout.spinner_dropdown_item, parent, false);
        }

        return convertView;
    }
}

您需要在适配器内的相应方法中设置视图的文本。