从onItemClickListener

时间:2015-06-06 07:57:14

标签: android json android-listview android-json

首先,我使用volley库作为我的帖子请求。在这种情况下,我从服务器检索以下json格式的响应。

{"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"}
          ]
}

我执行所有必需的JSON反序列化并在列表中显示团队数组的对象。

现在我想要做的是在String数组中存储所选团队的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表对应。

2 个答案:

答案 0 :(得分:1)

{&#34; DDL&#34;:[{&#34;标识&#34;:1,&#34;名称&#34;:&#34;阿鲁巴&#34;},{&#34; ID&#34;:2&#34;名称&#34;:&#34;阿富汗&#34;},{&#34; ID&#34;:3,&#34;名称&#34;:&#34 ;安哥拉&#34;},{&#34;标识&#34:4,&#34;名称&#34;:&#34;安圭拉&#34;},{&#34;标识&#34;:5, &#34;名称&#34;:&#34;阿尔巴尼亚&#34;},{&#34;标识&#34;:6,&#34;名称&#34;:&#34;安道尔&#34;}] }

上面的代码显示了Json数组,DDL是json数组标记名。

// Country Spinner

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) {

        }
    });
}

答案 1 :(得分:0)

您可以使用列表活动并在其中显示所有数据

    protected void onListItemClick(ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(MainActivity.this,NextActivity.class);
    startActivity(i);
}

可以从http://developer.android.com/reference/android/app/ListActivity.html

获取更多详情
相关问题