ANDROID>如何使用Spinner设置JSON数据 [英] ANDROID > How to set JSON Data with Spinner

查看:231
本文介绍了ANDROID>如何使用Spinner设置JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,抱歉,如果我在做白痴的问题,

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

对于Issues

我已经创建了一个类似于下面的应用程序,并且微调器中的所有值都通过JSON在链接中获得

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

但是我想设置是否有人选择"ardie halim"的第二个微调框仅显示移动开发人员",如果有人选择"indah"第二个的微调框,显示数据库oracle",依此类推

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

我试图从go * gle中找到该教程,但是我不知道该怎么找到正确的keyword

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

仅供参考,有关我的代码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
    }

});

这篇关于ANDROID&gt;如何使用Spinner设置JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆