英文:
How to define spinner by id and not by position
问题
private CategoriaNovo spinnerCategoria;
private List<CategoriaNovo> listaCategorias = new ArrayList<>();
// Spinner initialization
ArrayAdapter<CategoriaNovo> dataAdapter = new ArrayAdapter<CategoriaNovo>(this, android.R.layout.simple_spinner_item, listaCategorias);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
// Fetching category data from server
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url_listar_categoria, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
CategoriaNovo categoria = new CategoriaNovo();
try {
JSONObject jsonObject = response.getJSONObject(i);
categoria.setIdCategoria(jsonObject.getInt("idCategoria"));
categoria.setCategoria(jsonObject.getString("nomeCategoria"));
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Erro 01", Toast.LENGTH_SHORT).show();
}
listaCategorias.add(categoria);
}
carregarSpinner();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Erro 02", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(arrayRequest);
Please note that I have only provided the translated Java code portion as per your request. If you have any questions or need further assistance, feel free to ask.
英文:
The menu is not selecting the correct position of the registered category id
I am making a app of restaurant.
I have a spinner populate by category list.
When I go select a product I want to pull all informations of product, how name and category.
The problem is that I dont get to select category by id registered.
Example, I have these positions and these ids:
0 - 3(id)
1 - 2(id)
2 - 15(id)
3 - 10(id)
My menu is saved in the database, when I choose the menu I want to show on the screen its information, such as menu name, ingredients and its category. To search for the category I'm looking for the id_category.
I want that when I open the menu, the spinner is automatically filled in by the position of the id_category
private CategoriaNovo spinnerCategoria;
private List<CategoriaNovo> listaCategorias = new ArrayList<>();
My Spinner
ArrayAdapter<CategoriaNovo> dataAdapter = new ArrayAdapter<CategoriaNovo>(this,android.R.layout.simple_spinner_item, listaCategorias);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
How am I listing the data in the spinner
JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url_listar_categoria, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i = 0; i < response.length(); i++){
CategoriaNovo categoria = new CategoriaNovo();
try {
JSONObject jsonObject = response.getJSONObject(i);
categoria.setIdCategoria(jsonObject.getInt("idCategoria"));
categoria.setCategoria(jsonObject.getString("nomeCategoria"));
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),
"Erro 01",
Toast.LENGTH_SHORT).show();
}
listaCategorias.add(categoria);
}
carregarSpinner();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"Erro 02",
Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(arrayRequest);
专注分享java语言的经验与见解,让所有开发者获益!
评论