简体   繁体   中英

Using SearchableSpinner with Custom Adapter in Android

I am using ‍‍SearchableSpinner in my Android project and in my SearchableSpinner, I am using two textview so I made a custom adapter. When I click the SearchableSpinner, the data not showing properly.

纺纱机

获取数据没有问题,但是当我单击它选择任何项目时,问题将显示为

Here is my custom adapter code. I use getView and getDropDownview in my custom adapter.

private ArrayList<University> universityList;
private Activity context;

public UniversityListAdapterForUploadPage(ArrayList<University> universityList, Activity context){
    super(context,R.layout.university_list_row_for_spinner,universityList);
    this.universityList = universityList;
    this.context = context;
}

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

    LayoutInflater layoutInflater = context.getLayoutInflater();
    View customView = layoutInflater.inflate(R.layout.university_list_row_for_spinner,null,true);

    TextView universityIDText  = customView.findViewById(R.id.universityID_textView_for_spinner);
    TextView universityNameText = customView.findViewById(R.id.university_name_textView_for_spinner);

    University university = universityList.get(position);

    universityIDText.setText(university.getUniversityID()+"");
    universityNameText.setText(university.getUniversityName());

    return customView;
}

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater layoutInflater = context.getLayoutInflater();
    View customView = layoutInflater.inflate(R.layout.university_list_row_for_spinner,null,true);

    TextView universityIDText  = customView.findViewById(R.id.universityID_textView_for_spinner);
    TextView universityNameText = customView.findViewById(R.id.university_name_textView_for_spinner);

    University university = universityList.get(position);

    universityIDText.setText(university.getUniversityID()+"");
    universityNameText.setText(university.getUniversityName());

    return customView;
}

I need help.

universityIDText.setText(university.getUniversityID()+"");
universityNameText.setText(university.getUniversityName());

You're not passing a String with these two calls. You're passing a java object ID

@Override toString() in your Model Add this to your Model class.. Hope it will help ..

public String toString(){
    return universityName;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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