gpt4 book ai didi

android - 在微调器中设置键和值

转载 作者:IT王子 更新时间:2023-10-28 23:55:15 25 4
gpt4 key购买 nike

我有一个微调器,我想在上面设置一个键和一个值,我使用 HashMap,它可以工作,但显示一行,像这样:

enter image description here

代码:

        final View rootView = inflater.inflate(R.layout.fragment_photos, container, false);

Spinner spin=(Spinner)rootView.findViewById(R.id.spinner1);

HashMap<Integer, String> P_Hash=new HashMap<Integer, String>();

Update Get_Information=new Update(rootView.getContext());

ArrayList<String> Province_NAME=new ArrayList<String>();
Province_NAME=Get_Information.GET_Province();

ArrayList<Integer> Province_ID=new ArrayList<Integer>();
Province_ID=Get_Information.Get_Province_ID();

for (int i = 0; i < Province_ID.size(); i++)
{
P_Hash.put(Province_ID.get(i), Province_NAME.get(i));
Log.d("Province_ID.get(i)", Province_ID.get(i)+"");
Log.d(" Province_NAME.get(i)", Province_NAME.get(i)+"");
}


ArrayAdapter<HashMap<Integer, String>> adapter = new ArrayAdapter<HashMap<Integer,String>>(rootView.getContext(), android.R.layout.simple_spinner_item);
adapter.add(P_Hash);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(adapter);

最佳答案

用键和值填充微调器的更好方法应该是:

第 1 步:创建 POJO 类来处理键和值

public class Country {

private String id;
private String name;

public Country(String id, String name) {
this.id = id;
this.name = name;
}


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;
}


//to display object as a string in spinner
@Override
public String toString() {
return name;
}

@Override
public boolean equals(Object obj) {
if(obj instanceof Country){
Country c = (Country )obj;
if(c.getName().equals(name) && c.getId()==id ) return true;
}

return false;
}

}



注意: toString() 方法很重要,因为它负责在 spinner 中显示数据,您可以根据需要修改 toString()

第 2 步:准备要加载到微调器中的数据

 private void setData() {

ArrayList<Country> countryList = new ArrayList<>();
//Add countries

countryList.add(new Country("1", "India"));
countryList.add(new Country("2", "USA"));
countryList.add(new Country("3", "China"));
countryList.add(new Country("4", "UK"));

//fill data in spinner
ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(context, android.R.layout.simple_spinner_dropdown_item, countryList);
spinner_country.setAdapter(adapter);
spinner_country.setSelection(adapter.getPosition(myItem));//Optional to set the selected item.
}

第三步:最后在spinner的onitemselected监听器方法中获取选中项的key和value

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

Country country = (Country) parent.getSelectedItem();
Toast.makeText(context, "Country ID: "+country.getId()+", Country Name : "+country.getName(), Toast.LENGTH_SHORT).show();
}

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

关于android - 在微调器中设置键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24712540/

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