我如何使用列表视图从getItemAtPosition中选择的ID

时间:2018-07-11 15:26:46

标签: android

我当时正在使用微调器,并且一切工作都很好,但是我现在想使用listView,并且我不能发送在putextra中选择的ID,因为它是null。 这是我的课程

  // list func
    lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // String folder = lst.getItemAtPosition( lst.getSelectedItemPosition()).toString();

            String folder = (String) lst.getItemAtPosition(i);

            String id = lstMap.get(lst.getSelectedItemPosition());



            Toast.makeText(getApplicationContext(),"Vous avez choisis "+ id,Toast.LENGTH_LONG).show();

            String newReq= new StringBuilder().append(AppConfig.URL_GetFoldersFK).append(id).toString();
            Log.d("Request show",newReq);

            Intent intent = new Intent(MainActivity.this, BuildingsListActivity.class);
            // Sending value to another activity using intent.
            intent.putExtra("FolderId", newReq);
            startActivity(intent);
        }
    });

在这里我检索所有数据,并将ID存储在哈希图中,以便在上面的代码中使用

   // list function

    private void loadListFolders (String url) {
        RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");

                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String dossier = jsonObject1.getString("nom");
                        String id = jsonObject1.getString("id");

                        lstMap.put(i,id);
                        Folders.add(dossier);

                    }

                    // spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, Folders));
                    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
                            (MainActivity.this, android.R.layout.simple_list_item_1, Folders);

                    lst.setAdapter(spinnerArrayAdapter);

                }catch (JSONException e){
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }

1 个答案:

答案 0 :(得分:0)

尝试以下代码从哈希图中检索ID:

HashMap<int, Object> obj = (HashMap<int, Object>) adapterView.getAdapter().getItem(i);
String name = (String) obj.get(i);
Log.d("Yourtag", name);