android json spinner,android - Populating Spinner with JSON - Stack Overflow

From the link posted it looks like a incomplete tutorial.

//JSON Functions cannot be resolved Error occured

So the tutorial is missing JSONFunctions class and WorldPopulation class.

Have the below and keep the rest of the code the same.

private class DownloadJSON extends AsyncTask {

{

@Override

protected Void doInBackground(Void... params) {

world = new ArrayList();

worldlist = new ArrayList();

try

{

HttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpGet request = new HttpGet("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");

HttpResponse response = httpclient.execute(request);

HttpEntity resEntity = response.getEntity();

String _response=EntityUtils.toString(resEntity);

Log.i("Response is......................",""+_response);

jsonobject = new JSONObject(_response);

jsonarray = jsonobject.getJSONArray("worldpopulation");

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

jsonobject = jsonarray.getJSONObject(i);

WorldPopulation worldpop = new WorldPopulation();

worldpop.setRank(jsonobject.optString("rank"));

worldpop.setCountry(jsonobject.optString("country"));

worldpop.setPopulation(jsonobject.optString("population"));

worldpop.setFlag(jsonobject.optString("flag"));

world.add(worldpop);

worldlist.add(jsonobject.optString("country"));

}catch(Exception e)

{

}

return null;

}

Edit:

public class MainActivity extends Activity {

JSONObject jsonobject;

JSONArray jsonarray;

ProgressDialog mProgressDialog;

ArrayList worldlist;

ArrayList world;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

new DownloadJSON().execute();

}

private class DownloadJSON extends AsyncTask

{

@Override

protected Void doInBackground(Void... params) {

world = new ArrayList();

worldlist = new ArrayList();

try

{

HttpClient httpclient = new DefaultHttpClient();

httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpGet request = new HttpGet("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt");

HttpResponse response = httpclient.execute(request);

HttpEntity resEntity = response.getEntity();

String _response=EntityUtils.toString(resEntity);

Log.i("Response is......................",""+_response);

jsonobject = new JSONObject(_response);

jsonarray = jsonobject.getJSONArray("worldpopulation");

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

jsonobject = jsonarray.getJSONObject(i);

WorldPopulation worldpop = new WorldPopulation();

worldpop.setRank(jsonobject.optString("rank"));

worldpop.setCountry(jsonobject.optString("country"));

worldpop.setPopulation(jsonobject.optString("population"));

worldpop.setFlag(jsonobject.optString("flag"));

world.add(worldpop);

worldlist.add(jsonobject.optString("country"));

}

}catch(Exception e)

{

}

return null;

}

protected void onPostExecute(Void args) {

// Locate the spinner in activity_main.xml

Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);

// Spinner adapter

mySpinner

.setAdapter(new ArrayAdapter(MainActivity.this,

android.R.layout.simple_spinner_dropdown_item,

worldlist));

// Spinner on item click listener

mySpinner

.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override

public void onItemSelected(AdapterView> arg0,

View arg1, int position, long arg3) {

// TODO Auto-generated method stub

// Locate the textviews in activity_main.xml

TextView txtrank = (TextView) findViewById(R.id.rank);

TextView txtcountry = (TextView) findViewById(R.id.country);

TextView txtpopulation = (TextView) findViewById(R.id.population);

// Set the text followed by the position

txtrank.setText("Rank : "

+ world.get(position).getRank());

txtcountry.setText("Country : "

+ world.get(position).getCountry());

txtpopulation.setText("Population : "

+ world.get(position).getPopulation());

}

@Override

public void onNothingSelected(AdapterView> arg0) {

// TODO Auto-generated method stub

}

});

}

}

}

WorldPopulation.java

public class WorldPopulation {

String rank,country,population,flag;

public String getRank() {

return rank;

}

public void setRank(String rank) {

this.rank = rank;

}

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

public String getPopulation() {

return population;

}

public void setPopulation(String population) {

this.population = population;

}

public String getFlag() {

return flag;

}

public void setFlag(String flag) {

this.flag = flag;

}

}

activity_main.cml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/my_spinner"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/rank"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/my_spinner" />

android:id="@+id/country"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/rank" />

android:id="@+id/population"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/country" />

Snap

a06463fc5b958490b5f98ea72ed2b006.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值