gpt4 book ai didi

android - 如何删除微调器android中的默认值?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:00 26 4
gpt4 key购买 nike

我正在使用 JSON 从数据库中获取值并将它们插入到微调器中。此过程正在成功运行。但是当我点击微调器时,我收到了两次微调器提示;如何从微调器中删除第一个默认值...

这是快照:- 希望你能理解我的问题:enter image description here

这是我的代码:-

private class GetCategories extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler jsonParser = new ServiceHandler();
String json = jsonParser.makeServiceCall(URL_CATEGORIES,
ServiceHandler.GET);
if (json != null) {
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONArray categories = jsonObj
.getJSONArray("categories");
categoriesList.clear();
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(
catObj.getString("bus_type_id"),
catObj.getString("bus_type"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("JSON Data", "Didn't receive any data from server!");
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
populateSpinner();
}
}




private void populateSpinner() {
List<String> lables = new ArrayList<String>();
lables.add("Choose Type");
for (int i = 0; i < categoriesList.size(); i++) {
lables.add(categoriesList.get(i).getbus_type());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
typeSpinner.setAdapter(spinnerAdapter);
typeSpinner.setOnItemSelectedListener(typeSelectedListener);
}

private OnItemSelectedListener typeSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (typeSpinner.getSelectedItem().toString() == "Choose Type") {
str = "";
typeSpinnercity.getSelectedView();
typeSpinnercity.setEnabled(false);
} else {
ok.setEnabled(true);
str = typeSpinner.getSelectedItem().toString();
typeSpinnercity.getSelectedView();
typeSpinnercity.setEnabled(true);
if (Utils.checkInternet(CutomerOfferActivity.this)) {
new GetCategoriesCity().execute();
}
}
}

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

最佳答案

使用 ArrayAdapter 并覆盖方法 - getDropDownView..

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables){
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

View v = null;

if (position == 0) {
TextView tv = new TextView(getContext());
tv.setHeight(0);
tv.setVisibility(View.GONE);
v = tv;
}
else {

v = super.getDropDownView(position, null, parent);
}

parent.setVerticalScrollBarEnabled(false);
return v;
}
};

使用上面提到的代码并删除下拉列表中的默认值。

关于android - 如何删除微调器android中的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967682/

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