-1

My server json data is like:--[]

public class Main2Activity extends Activity {
        final Context context = this;

        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {

        }
    }

Here I can Fetch all the country name.but Rank is not coming on text-view.where is the problem??can anyone tell me????????

user_apr
  • 719
  • 2
  • 10
  • 27

4 Answers4

0

You are using wrong key 'name'

txt1.setText("Rank- "+jsonObject.getString("name"));

Use this line

txt1.setText("Rank- "+jsonObject.getString("Rank"));

You have to create listener for getting item selected event of spinner.

Refer this link and check for code in setOnItemClickListener()

Add comment if you find some difficulty.

Community
  • 1
  • 1
Paritosh
  • 2,097
  • 3
  • 30
  • 42
0

Try to replace this code :

txt1.setText("Rank- "+jsonObject.getString("name"));

With this code :

 txt1.setText("Rank- "+jsonObject.getString("Rank"));

Note : you have given wrong key reference to get Rank value
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

First add to your List where you are parse your data

nameList.add(jsonObject.getString("Rank"));

You are using wrong key 'name'

txt1.setText("Rank- "+jsonObject.getString("name"));

Use this line onItemSelectedListener of Spinner.

txt1.setText("Rank- "+nameList.get(position).get("Rank"));

Change here:

 for (int i = 0; i < jsonArray.length(); i++) {
       JSONObject jsonObject = jsonArray.getJSONObject(i);
       nameList.add(jsonObject.getString("name"));
       nameList.add(jsonObject.getString("Rank"));

  }
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • I have used this..But It is fetching only one rank of the country.It is not showing the Rank of the corresponding country name. – user_apr Aug 23 '14 at 04:54
  • Can You please suggest Where I have to put public void onItemSelected(AdapterView> parent, View view, int pos, long id) {} in my program???I cant understand – user_apr Aug 23 '14 at 05:02
  • After populated your data in spinner. where you set your arraylist to your adapter. in onCreate() method. – Piyush Aug 23 '14 at 05:04
  • I have added ..But still showing error in nameList and position ..Can I send you the code?? – user_apr Aug 23 '14 at 05:23
  • Make this List nameList = new ArrayList(); global varibale. And in your spinner onItemSelectedListener method there is one int varibale so you have to pass that instead of position. – Piyush Aug 23 '14 at 05:24
0

Try this:

int cnt= jsonarr.length();
String country_name;
String country_rank;                    

for(int i=0;i<cnt;i++){
    country_name= jsonarr.getJSONObject(i).getString("name");
    country_rank= jsonarr.getJSONObject(i).getString("Rank");
}

tv.setText(country_rank.toString());

Hope this may help you!

Paritosh
  • 2,097
  • 3
  • 30
  • 42
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28