问题 我正在使用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>
答案 0 :(得分:2)
在名为<div id="principal">
<div id="footer">
<ul id="menu3">
<li><a href="#"><img src="images/responsabilidade.png" /><span>responsabilidade social</span></a></li>
<li><a href="#"><img src="images/horario.png" /> <span>horário</span></a></li>
<li><a href="#"><img src="images/localizacao.png" /> <span>localização</span></a></li>
<li><a href="#"><img src="images/contactos.png" /> <span>contactos</span></a></li>
<li><a href="#"><img src="images/mapaSite.png" /> <span>mapa do site</span></a></li>
<li><a href="#"><img src="images/creditos.png" /> <span>créditos</span></a></li>
</ul>
</div>
</div>
的布局文件夹中创建xml文件,并在其中写下代码
item_spinner
并在创建适配器时更改代码。
<?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();
以编程方式将适配器设置为ListView
或Spinner
并使用ArrayAdapter
或布局中的任何其他版本,那么它会设置项目的文字颜色为白色。我没有理解它为什么默认情况下将白色设置为文本。