gpt4 book ai didi

java - AutoCompleteTextView - 选择名称时获取 Id

转载 作者:行者123 更新时间:2023-12-02 09:51:07 25 4
gpt4 key购买 nike

我从 api 获取值(我存储在模型中的带有 Id 的名称列表)- 如何将此名称设置为自动完成并在下拉选择中同时获取名称和 Id。这将在自动完成中设置名称并在 onItemClick 处获取名称,但如何获取 ID?

模型类

public class MeetingContactModel implements Serializable {

private String id;
private String text;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

MyActivity 类创建时:调用自动完成适配器

setMeetingContactAuto(autoContact, contactList);

autoContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
strContact =(String) parent.getItemAtPosition(position);
// strContactCode = code.get(position);
}
});

private void setMeetingContactAuto(AutoCompleteTextView autoContact, final ArrayList<MeetingContactModel> xcontactList) {

List<String> names = new AbstractList<String>() {
@Override
public int size() { return xcontactList.size(); }

@Override
public String get(int i) {
code.clear();
code.add(xcontactList.get(i).getText());
return xcontactList.get(i).getText();
}
};
autoContact.setThreshold(1);
autoContact.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
}

无法获取 ID - 请让我知道以获取它

最佳答案

在模型类中实现 toString() 方法

 public class MeetingContactModel implements Serializable {

private String id;
private String text;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

@Override
public String toString() {
return text;
}

}


// Fetch your selected model
autoContact.setAdapter(new ArrayAdapter<>(requireContext(), R.layout.spinner_item_ranking, contactList));
autoContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MeetingContactModel m=(MeetingContactModel) parent.getItem(position);
String name=m.getText();
String id=m.getId();
}
});

关于java - AutoCompleteTextView - 选择名称时获取 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com