2015-06-06 33 views
-3

首先我使用volley库来处理我的发布请求。在这种情况下,我从服务器获取以下json格式的响应。从onItemClickListener中获取来自JSON对象的ID

{"status":"success","message":"Teams without a league have been 
found.", 
"teams":[{"ID":"31","team_name":"A Team"}, 
      {"ID":"101","team_name":"The BEST team"}, 
      {"ID":"109","team_name":"ael fc"}, 
      {"ID":"110","team_name":"UK"}, 
      {"ID":"111","team_name":"cyprus"},    
      {"ID":"112","team_name":"biochemisty"} 
      ] 
} 

我做的所有necessaray JSON反序列化和dispay列表中的球队阵列的对象。

现在我想要做的就是将所选团队的ID值存储在字符串数组中。有关于此的任何想法?

下面是代码

final JsonObjectRequest jsonObjReq1 = new 
JsonObjectRequest(AppConfig.URL_GET_ALL_LEAGUE_TEAMS, jsonObject, 
      new com.android.volley.Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        Log.d("TAG", response.toString()); 

         try { 

      if(response.getString("status").equals("success")){ 

      JSONArray teamsArray = response.getJSONArray("teams"); 


         for(int i = 0; i< teamsArray.length(); i++){ 

           teams = teamsArray.getJSONObject(i); 

           noLeagueMembersClass = new 
         NoLeagueMemberClass();    
       noLeagueMembersClass. 
       setTeamMember(teams.getString("team_name")); 

       noLeagueMembersClass. 
       setTeamMember(teams.getString("ID"));      
       noLeagueMemberList.add(noLeagueMembersClass); 
           listView.setAdapter(noLeagueAdapter); 

       listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
           listView.setItemsCanFocus(false); 

           listView.setOnItemClickListener(new 
       AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick 
      (AdapterView<?> parent, View view, int position, long id){ 

      Toast.makeText(getApplicationContext(),"You 
      clicked"+position,Toast.LENGTH_SHORT).show(); 

            } 
           }); 
      } 



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

的一部分,我尝试以下

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 

             try { 

              final String teamId = 
              teams.getString("ID"); 
              Log.d("teamId",teamId); 
             } catch (JSONException e) { 
              e.printStackTrace(); 
             } 

            } 

,但我总是得到teamId = 120。例如,当我选择第一行时,我希望teamId等于31.当我单击第二行时,我希望teamId与101相等,依此类推。我不想将任何值传递给下一个活动。我希望下面的图片能让你明白我想要做什么。

enter image description here

换句话说,我希望每个点击我做对应JSON表。

+0

没有得到明确的想法。你想从列表视图中选择id或什么? – Pankaj

+0

我想从列表视图中选择团队的ID。它显示json – Theo

+0

设置NoLeagueMemberClass的数组列表,然后使用for循环在该数组列表中添加对象。 – Pankaj

回答

1

{ “DDL” 得到更多的细节:[{ “ID”:1,”命名 “:” 阿鲁巴 “},{” ID “:2”,名称 “:” 阿富汗 “},{” ID “:3”,名称 “:” 安哥拉 “},{” ID “:4,” 姓名” :“安圭拉”},{“Id”:5,“name”:“Albania”},{“Id”:6,“name”:“Andorra”}]}

上面的代码显示了Json数组而DDL是json数组的标签名称。

//国家微调

private void getCountry(){ 

    StringRequest stringRequest = new StringRequest(Request.Method.GET,"URL", 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        try { 



         //Parsing the fetched Json String to JSON Object 
         JSONObject j = new JSONObject(response); 

         //Storing the Array of JSON String to our JSON Array 
         countryarray = j.getJSONArray("DDL"); 

         //Calling method getStudents to get the students from the JSON Array 
         getCountries(countryarray); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 

    //Creating a request queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

    //Adding request to the queue 
    requestQueue.add(stringRequest); 
} 
private void getCountries(final JSONArray jsonArrayC){ 
    //Traversing through all the items in the json array 

    countrylist=new ArrayList<String>(); 
    for(int i=0;i<jsonArrayC.length();i++){ 
     try { 
      //Getting json object 
      JSONObject json = jsonArrayC.getJSONObject(i); 
      countryid= Integer.valueOf(json.getString("Id")); 

      //Adding the name of the emtype to array list 
      countrylist.add(json.getString("name")); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    countryspinner.setAdapter(new ArrayAdapter<String>(AddEmployee.this, android.R.layout.simple_spinner_dropdown_item, countrylist)); 


    countryspinner.setSelection(99); 

    countryspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      countryid = (int) id + 1; 
      getState((int) id + 1); 


     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 
} 
+0

这个例子将帮助你从上面的问题 –

+0

请添加更详细的解释 - http://stackoverflow.com/help/how-to-answer –

+0

你期待什么? –