如何根据 ID 而不是位置来定义旋转组件。

huangapple 未分类评论45阅读模式
英文:

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.

英文:

Menu screen

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&lt;CategoriaNovo&gt; listaCategorias = new ArrayList&lt;&gt;();

My Spinner

ArrayAdapter&lt;CategoriaNovo&gt; dataAdapter = new ArrayAdapter&lt;CategoriaNovo&gt;(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&lt;JSONArray&gt;() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i = 0; i &lt; response.length(); i++){
                            CategoriaNovo categoria = new CategoriaNovo();
                            try {

                                JSONObject jsonObject = response.getJSONObject(i);

                                categoria.setIdCategoria(jsonObject.getInt(&quot;idCategoria&quot;));
                                categoria.setCategoria(jsonObject.getString(&quot;nomeCategoria&quot;));

                            } catch (JSONException e) {
                                Toast.makeText(getApplicationContext(),
                                        &quot;Erro 01&quot;,
                                        Toast.LENGTH_SHORT).show();
                            }
                            listaCategorias.add(categoria);
                        }
                        carregarSpinner();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),
                        &quot;Erro 02&quot;,
                        Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(arrayRequest);

huangapple
  • 本文由 发表于 2020年4月9日 21:02:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61121807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定