问题
我正在使用Retrofit从
mysql中获取一些产品类别.数据将在微调器中传入和分配,但是当我选择一个未显示的项目时.在下拉菜单中,项目已分配,setOnItemSelected侦听器也在工作.但是所选项目没有显示在微调器中.
我已经尝试了几乎所有相关的stackoverflow问题,但没有工作.请帮助我.提前致谢.
码
这是OrderActivity
public class OrderActivity extends AppCompatActivity {
Spinner sp_category,sp_product;
public static final String ROOT_URL = "http://10.0.2.2/sizzle/";
private List<Product> books;
List<String> prod_cat = new ArrayList<String>();
ArrayAdapter<String> adapter_prod_cat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
//sp_product = (Spinner)findViewById(R.id.spinner_product);
getBooks();
sp_category = (Spinner)findViewById(R.id.spinner_category);
sp_category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String category = parent.getSelectedItem().toString();
Toast.makeText(getApplicationContext(),category,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void getBooks(){
final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL)
.build();
ProductAPI api = adapter.create(ProductAPI.class);
api.getProduct(new Callback<List<Product>>() {
@Override
public void success(List<Product> list, Response response) {
//Dismissing the loading progressbar
loading.dismiss();
books = list;
for (int i = 0; i < books.size(); i++) {
prod_cat.add(books.get(i).getProductCategory());
System.out.println("added");
}
Set<String> citySet = new HashSet<String>(prod_cat);
prod_cat.clear();
prod_cat.addAll(citySet);
prod_cat.add("Select Category");
showListinSpinner();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
});
}
private void showListinSpinner(){
//String array to store all the book names
String[] items = new String[prod_cat.size()];
//Traversing through the whole list to get all the names
for(int i=0; i<prod_cat.size(); i++){
//Storing names to string array
items[i] = prod_cat.get(i);
}
//Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
//setting adapter to spinner
adapter.notifyDataSetChanged();
sp_category.setAdapter(adapter);
//Creating an array adapter for list view
}
}
这是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.skipper.ash.ashtet.OrderActivity">
<Spinner
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/spinner_category"
android:spinnerMode="dropdown"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:textAlignment="center"
android:visibility="visible" />
</RelativeLayout>
最佳答案 在名为item_spinner的布局文件夹中创建xml文件,并在其中编写下面的代码
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#000000" >
并在创建适配器时更改代码.
adapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.item_spinner, items);
也改变了陈述的顺序
sp_category.setAdapter(adapter);
adapter.notifyDataSetChanged();
首先将适配器设置为微调器,然后触发数据集更改事件.
希望它能帮到你.
编辑
I observed lot of time that if we set the adapter to
ListView
orSpinner
programatically usingArrayAdapter
and usesandroid.R.layout.simple_spinner_item_1
or any other build in layout, then it sets the text color of item to white. I didn’t get it why it sets the white color to text by default.