ANDROID>如何使用微调器

时间:2016-10-28 08:42:52

标签: android json android-spinner

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

代表Issues

我已经创建了一个类似下面的应用程序,以及JSON在此link

中获取的微调器中的所有值

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

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

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

  

关于我的代码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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

alter table product drop product_image_path;

答案 1 :(得分:1)

以下代码用于将Json数据设置为微调器

try {

       Gson gson = new Gson();
       String json = gson.toJson(response.body());

       JSONObject jsonObject = new JSONObject(json);

       Log.d("check", "jsonData : " + json);


       List<String> allGoverNames = new ArrayList<String>();
       allGoverNames.add(0, "Select Governorate");


       JSONArray cast = jsonObject.getJSONArray("governorate");

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

               JSONObject actor = cast.getJSONObject(i);
               governorateNamenameString = actor.getString("governorate_name");
               allGoverNames.add(governorateNamenameString);

               }

        GoverdataAdapter = new ArrayAdapter<String>
                                (context, android.R.layout.simple_spinner_item, allGoverNames);

        GoverdataAdapter.setDropDownViewResource
                                (android.R.layout.simple_spinner_dropdown_item);

        GovernorateSpinner.setAdapter(GoverdataAdapter);


     } catch (JSONException e) 
             {
              e.printStackTrace();
             }