gpt4 book ai didi

java - 如何根据值JSON设置选择项?

转载 作者:行者123 更新时间:2023-12-01 18:35:38 24 4
gpt4 key购买 nike

我有两个 JSON 数据,一个是用户的 JSON,另一个是微调器的 JSON:

这是用户的 JSON

{
"taxonomy":{
"User":[
{
"ID":112,
"name":"Tanaka",
"city":12

},
{
"ID":113,
"name":"Sugimoto",
"city":12
}
]
}
}

这是 Spinner 的 JSON

{
"taxonomy":{
"City":[
{
"city_id":12,
"name":"Jakarta"

},
{
"city_id":13,
"name":"Jakarta"
}
]
}
}

我已经将 JSON 解析为 spinner

for (int i=0; i<data_city.length();i++){
JSONObject listcity = data_city.getJSONObject(i);
Spin_city modelcity = new Spin_kecamatandesa(
listkec.getInt("city_id"),
listkec.getString("name")
);

Str_array_kec.add(modelcity);
}

SpinnAdapter adapterspin = new SpinnAdapter(getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item, Str_array_kec);
spn_edt_kec.setAdapter(adapterspin);

这是我的适配器

public class SpinnAdapter extends ArrayAdapter<Spin_city> {
private ArrayList<Spin_city> myarrayList;

public adapterDesa(@NonNull Context context, int textViewResourceId, ArrayList<Spin_city> modelarrayList) {
super(context, textViewResourceId, modelarrayList);
this.myarrayList = modelarrayList;
}

@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getCustomView(position, parent);
}

@Nullable
@Override
public Spin_city getItem(int position) {
return myarrayList.get(position);
}

@Override
public int getCount() {
int count = myarrayList.size();

return count;
}


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

private View getCustomView(int position, ViewGroup parent) {
Spin_city model = getItem(position);

View spinnerRow = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
TextView label = spinnerRow.findViewById(android.R.id.text1);
label.setText(String.format("%s", model != null ? model.getName_city(): ""));
return spinnerRow;
}

我的问题是如何根据 JSON 用户在微调器上设置选择项(例如“city”等于“city_id”),谁能帮忙这个

最佳答案

要检查用户和微调器 JSON 键的相等性,您必须将两个 ArrayList 传递到适配器,以便可以检查相等的值

关于java - 如何根据值JSON设置选择项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60054756/

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