【问题标题】:Getting id from from JSON objects inside onItemClickListener从 onItemClickListener 中的 JSON 对象获取 id
【发布时间】:2015-08-21 05:20:38
【问题描述】:

首先,我将 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 反序列化并在列表中显示团队数组的对象。

现在我要做的是将所选团队的 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,依此类推。我还不想将任何值传递给下一个活动。我希望下面的图片能让你明白我想要做什么。

换句话说,我希望我所做的每次点击都对应于 JSON 表。

【问题讨论】:

  • 没有清楚的想法。您想在从列表视图中选择时获取 id 还是什么???
  • 我想在从列表视图中选择时获取团队的 ID。显示为 json
  • 创建一个 NoLeagueMemberClass 的数组列表,然后使用您正在执行的 for 循环在该数组列表中添加对象。
  • 我有你所说的 ArrayList。我想我必须在 onItemClick 方法中做一些事情。但我不知道是什么。正如我所说,当我单击列表视图的一行时,我想要真正的团队 ID。所以需要在那里做点什么。

标签: android json android-listview android-json


【解决方案1】:

{"DDL":[{"Id":1,"name":"Aruba"},{"Id":2,"name":"阿富汗"},{"Id":3,"name ":"安哥拉"},{"Id":4,"name":"Anguilla"},{"Id":5,"name":"阿尔巴尼亚"},{"Id":6,"name": "安道尔"}]}

以上代码显示的是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) {

        }
    });
}

【讨论】:

  • 这个例子将帮助你解决上述问题
  • 请添加更详细的解释-stackoverflow.com/help/how-to-answer
  • 你期待什么?
  • 不要只发布代码 - 解释为什么这个解决方案有效!
  • do yoy got it..first code是json数组,ddl是json标签名,使用belove代码检索我们从spinner中选择的iem的id
【解决方案2】:

您可以使用列表 Activity 并在其中显示所有数据,然后

    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获取更多详情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2014-02-08
    • 2022-01-24
    相关资源
    最近更新 更多