【问题标题】:Spinner with custom array adapter带有自定义阵列适配器的微调器
【发布时间】:2018-09-14 09:54:21
【问题描述】:

我正在尝试从自定义数组适配器填充微调器。一切正常,除非您单击微调器选择一个新值,它会为每个项目显示一长串代码。但是,在单击长长的代码行后会显示正确的名称并给出正确的值。

它的作用示例

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

自定义数组适配器

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;
        }
    }

公共类 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;
}

}

【问题讨论】:

  • 您的CompanyClass 是否提供toString() 覆盖?
  • 也覆盖getDropDownView()
  • @IvanTodorović 我在编辑中加入了课程,谢谢
  • @pskink 我需要在哪里覆盖 getDropDownView(),你能提供一个例子吗?谢谢
  • ArrayAdapter#getDropDownView 或如 Ivan 所说,只需覆盖 CompanyClass#toString() 方法(例如仅返回 Name

标签: android class spinner android-arrayadapter custom-arrayadapter


【解决方案1】:

试试这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多