ANDROID>如何使用微调器设置JSON数据(ANDROID > How to set JSON Data with Spinner)

我是新来的,对不起,如果我做一些白痴问题,

为Issues

我已经创建了一个如下所示的应用程序,以及JSON在此链接中获得的微调器中的所有值

当应用程序运行时,会发生这种情况, Spinner

但是我想设置是否有人选择“ardie halim”第二个微调只显示“移动开发人员”,如果有人选择“indah”显示“数据库oracle”的第二个微调,等等

我试图从go * gle找到教程,但我不知道什么是正确的keyword找出来的,

关于我的代码MainActivity.java FYI

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<String> listItems=new ArrayList<>();
    ArrayAdapter<String> adapter;
    Spinner sp;
    ArrayList<String> listItems2=new ArrayList<>();
    ArrayAdapter<String> 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<Void,Void,Void> {
        ArrayList<String> list;
        ArrayList<String> 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<jArray.length();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();
        }
    }
}

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<String> listItems=new ArrayList<>();
    ArrayAdapter<String> adapter;
    Spinner sp;
    ArrayList<String> listItems2=new ArrayList<>();
    ArrayAdapter<String> 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<Void,Void,Void> {
        ArrayList<String> list;
        ArrayList<String> 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<jArray.length();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();
        }
    }
}

                

最满意答案

sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { // your code here if(list.get(position).equals("ardie halim")){ listItems2.clear(); listItems2.add("mobile developer"); adapter2.notifyDataSetChanged(); } else if(list.get(position).equals("indah")){ listItems2.clear(); listItems2.add("database oracle"); adapter2.notifyDataSetChanged(); } } @Override public void onNothingSelected(AdapterView<?> parentView) { // your code here } }); sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { // your code here if(list.get(position).equals("ardie halim")){ listItems2.clear(); listItems2.add("mobile developer"); adapter2.notifyDataSetChanged(); } else if(list.get(position).equals("indah")){ listItems2.clear(); listItems2.add("database oracle"); adapter2.notifyDataSetChanged(); } } @Override public void onNothingSelected(AdapterView<?> parentView) { // your code here } });

更多推荐