android json spinner,ANDROID > How to set JSON Data with Spinner

im new here, sorry if i doing something idiot question,

for the Issues

i already create an app like that below, and all the value in the spinner obtained by JSON in this link

and this happens when the application running,

Spinner

but i want to set if someone choose "ardie halim" the 2nd spinner just show "mobile developer", and if someone choose "indah" the 2nd spinner showing "database oracle", and so on

i tried to find the tutorial from go*gle, but i dunno what the right keyword to find out,

FYI about my code MainActivity.java

import android.os.AsyncTask;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.widget.ArrayAdapter;

import android.widget.Spinner;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ArrayList listItems=new ArrayList<>();

ArrayAdapter adapter;

Spinner sp;

ArrayList listItems2=new ArrayList<>();

ArrayAdapter adapter2;

Spinner sp2;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sp=(Spinner)findViewById(R.id.spinner);

sp2=(Spinner)findViewById(R.id.spinner2);

adapter= new ArrayAdapter<>(this, R.layout.spinner_layout, R.id.txt, listItems);

adapter2= new ArrayAdapter<>(this, R.layout.spinner_layout, R.id.txt, listItems2);

sp.setAdapter(adapter);

sp2.setAdapter(adapter2);

}

public void onStart(){

super.onStart();

BackTask bt=new BackTask();

bt.execute();

}

private class BackTask extends AsyncTask {

ArrayList list;

ArrayList list2;

protected void onPreExecute(){

super.onPreExecute();

list=new ArrayList<>();

list2=new ArrayList<>();

}

protected Void doInBackground(Void...params){

InputStream is=null;

String result="";

try{

HttpClient httpclient=new DefaultHttpClient();

HttpPost httppost= new HttpPost("http://zxccvvv.netne.net/dosen.php");

HttpResponse response=httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

// Get our response as a String.

is = entity.getContent();

}catch(IOException e){

e.printStackTrace();

}

//convert response to string

try{

BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));

String line;

while ((line = reader.readLine()) != null) {

result+=line;

}

is.close();

//result=sb.toString();

}catch(Exception e){

e.printStackTrace();

}

// parse json data

try{

JSONArray jArray =new JSONArray(result);

for(int i=0;i

JSONObject jsonObject=jArray.getJSONObject(i);

// add interviewee name to arraylist

list.add(jsonObject.getString("nama_dosen"));

list2.add(jsonObject.getString("mat_kul"));

}

}

catch(JSONException e){

e.printStackTrace();

}

return null;

}

protected void onPostExecute(Void result){

listItems.addAll(list);

listItems2.addAll(list2);

adapter.notifyDataSetChanged();

adapter2.notifyDataSetChanged();

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值