简体   繁体   English

带自定义阵列适配器的微调器

[英]Spinner with custom array adapter

I am trying to populate a spinner from a custom array adapter. 我正在尝试从自定义阵列适配器填充微调器。 Everything works perfectly except when you click on the spinner to choose a new value it displays a long line of code for each item. 除了单击微调器以选择新值之外,其他所有选项均会显示一长行代码,因此一切工作均正常。 However, after clicking on the long line of code displays the correct name and gives the correct value though. 但是,单击较长的代码行后,将显示正确的名称并给出正确的值。

Example of what it does 它做什么的例子

https://i.stack.imgur.com/AhUqw.png https://i.stack.imgur.com/AhUqw.png

Custom array adapter 自定义阵列适配器

private void populateCompanyList()
    {
        ArrayAdapter<CompanyClass> Adapter = new OnlyListAdapter();
        Company.setAdapter(Adapter);
    }

    private class OnlyListAdapter extends ArrayAdapter<CompanyClass>
    {
        public OnlyListAdapter() {
            super(getActivity(), R.layout.spinner_item, listCompany);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //Make sure we have a view to work with
            View itemView = convertView;
            if (itemView == null)
                itemView = getActivity().getLayoutInflater().inflate(R.layout.spinner_item, parent, false);

            CompanyClass currentCompany = listCompany.get(position);

            TextView Name = (TextView) itemView.findViewById(R.id.txtName);
            Name.setText(currentCompany.getName());
            Toast.makeText(getActivity(), currentCompany.getName(), Toast.LENGTH_LONG).show();


            return itemView;
        }
    }

public class CompanyClass { 公共类CompanyClass {

String ID;
String Name;

public CompanyClass(String ID, String Name)
{
    this.ID=ID;
    this.Name=Name;
}

public String getID() {
    return ID;
}

public String getName() {
    return Name;
}

} }

Try this out. 试试看

  @Override
    public String toString() {
        return this.Name;          
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM