gpt4 book ai didi

java - 如何在微调器中选择项目后获取微调器值

转载 作者:行者123 更新时间:2023-12-05 00:19:23 24 4
gpt4 key购买 nike

我在我的应用程序中使用微调器我正在使用从服务器获取的数据填充微调器。我使用 Retrofit2 作为网络库。我从服务器获取了 2 个值,一个是状态名称,另一个是状态 ID。
在微调器中,我显示状态名称,但在选择状态时,它应该选择我从服务器获取的相应状态名称 ID。在微调器中,状态名称显示成功,但我想获取我在 POJO 类中拥有的相应状态名称 ID,而不是项目位置。
下面是我的代码:
服务器响应如下:

{
"data": [
{
"id": "5",
"name": "Bihar"
},
{
"id": "7",
"name": "Chhattisgarh"
},
{
"id": "10",
"name": "Delhi"
}
],
"status": true,
"code": 200
}


States.java
public class States {

@SerializedName("data")
@Expose
private List<AllStates> data = null;
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("code")
@Expose
private int code;

public States(){

}

public List<AllStates> getData() {
return data;
}

public void setData(List<AllStates> data) {
this.data = data;
}

public Boolean getStatus() {
return status;
}

public void setStatus(Boolean status) {
this.status = status;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
}
AllStates.java
public class AllStates {

@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;

public AllStates(){

}

public String getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
CalenderFragment.java
public class CalendarFragment extends Fragment {

Spinner spinnerState;
List<AllStates> stateList = new ArrayList<>();
ArrayList<String> stateSpinnerList = new ArrayList<>();

public CalendarFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_calendar, container, false);

spinnerState = view.findViewById(R.id.spinnerState);

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

String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}

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

}
});

Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);

apiService.allStates().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<States>() {
@Override
public void onSubscribe(Disposable d) {

}

@Override
public void onNext(States states) {

stateList = states.getData();

stateSpinnerList.add("Select state");

for(int i =0;i<stateList.size();i++){

stateSpinnerList.add(stateList.get(i).getName());
}

ArrayAdapter<String> stateAdapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,stateSpinnerList);
spinnerState.setAdapter(stateAdapter);
}

@Override
public void onError(Throwable e) {

TastyToast.makeText(getActivity(),e.getMessage(),TastyToast.LENGTH_SHORT,
TastyToast.ERROR).show();
}

@Override
public void onComplete() {

}
});

return view;
}
}
我怎样才能得到想要的结果?

最佳答案

所以我的理解是您正在从 Spinner 中选择状态并获取状态名称,但您想要获取 stateId。
我能想到的一种可能的解决方案是使用 HashMap 映射。
当您从 spinner 收到状态名称时,只需调用 map.get(name),您将获得状态 ID。
编辑:
你可以像这样实现它

HashMap<String, Integer> map = new HashMap<>();
for(state in list){
map.put(state.name, state.id);
}

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

String name = parent.getItemAtPosition(position).toString();
int id = map.get(name);
}

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

}
});

关于java - 如何在微调器中选择项目后获取微调器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63086853/

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