-3

First of all I use volley library for my post request. In this case I retrieve as response from the server the following json format.

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

I do all the necessaray JSON deserialization and dispay the objects of the team array in a list.

Now what I want to do is to store in a String array the ID values of the selected teams. Any ideas on that?

Here is the part of the code

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();
    }

I try the following

 @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();
                                         }

                                     }

but I always get teamId=120. For example when I select first row,I want teamId to be equals with 31. When I click the second row I want the teamId to be equal with 101 and so on. I don't want to pass any values to a next activity yet. I hope the following picture will get you to understand what I want to do.

enter image description here

In other words I want each click I do to correspond to the JSON table.

Theo
  • 3,099
  • 12
  • 53
  • 94
  • Didn't get the clear idea. You want to fetch id on selecting from listview or what??? – Pankaj Jun 06 '15 at 08:10
  • I want to fetch the id of the team on selecting from listview. It is shown json – Theo Jun 06 '15 at 08:13
  • Make a arraylist of NoLeagueMemberClass and then add object in that arraylist using for loop which you are doing. – Pankaj Jun 06 '15 at 08:27
  • I have the ArrayList you are talking about. I think I have to do something in onItemClick method. But I don't know what. As I said I want to real team's id when I click a row of the listview. So something needs to be done there. – Theo Jun 06 '15 at 08:38

2 Answers2

1

{"DDL":[{"Id":1,"name":"Aruba"},{"Id":2,"name":"Afghanistan"},{"Id":3,"name":"Angola"},{"Id":4,"name":"Anguilla"},{"Id":5,"name":"Albania"},{"Id":6,"name":"Andorra"}]}

The above code shows the Json Array and DDL is the json array tag name.

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

        }
    });
}
Raseem Ayatt
  • 970
  • 10
  • 14
0

You can use list Activity and display all the data into it and after that

    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);
}

Can get more details from http://developer.android.com/reference/android/app/ListActivity.html

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35