gpt4 book ai didi

java - 如何在数组中获取动态微调器选择的项目 ID?

转载 作者:行者123 更新时间:2023-11-30 10:13:49 26 4
gpt4 key购买 nike

我已经完美地创建了动态微调器,如下所示

        k++;
flag=k;
final LinearLayout.LayoutParams lparams = new
LinearLayout.LayoutParams(380,120);
lparams.setMargins(1, 20, 1, 0);
colorSpinner[flag] = new Spinner(getActivity());
colorSpinner[flag].setLayoutParams(lparams);
colorSpinner[flag].setId(flag);
colorSpinner[flag].setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, color_list));

loadColorSpinnerData(URL);

colorSpinner[flag].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
color_id = datalist1.get(i).getId();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});

mLayout.addView(colorSpinner[flag]);

color_array.add(colorSpinner[flag]);

如何获取数组中的颜色id

这不行

String[] Acolorid = new String[color_array.size()];
for(int i = 0; i < color_array.size(); i++){
Acolorid[i] = String.valueOf(color_array.get(i).getSelectedItemPosition()+1); //Acolorid[j]= String.valueOf(color_id);//color_array.get(i).getSelectedItem().toString();
Hcolorid=String.join(",",Acolorid);
}

请给我解决方案,我想获取数组中的颜色 ID 而不是获取项目位置。

最佳答案

一种方法是使用 View.setTag()存储您以后要检索的数据。像这样的东西:

     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
// get some data related to the selected item
color_id = datalist1.get(i).getId();
// attach it to the spinner
adapterView.setTag(color_id);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}

稍后检索:

String[] Acolorid = new String[color_array.size()];
for(int i = 0; i < color_array.size(); i++){
// get the saved data
Object data = color_array.get(i).getTag();
if (data != null) {
Acolorid[i] = data.toString();
}
}
Hcolorid=String.join(",",Acolorid);

关于java - 如何在数组中获取动态微调器选择的项目 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166857/

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