Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将数据从json获取到spinner?_Java_Android_Android Volley - Fatal编程技术网

Java 如何将数据从json获取到spinner?

Java 如何将数据从json获取到spinner?,java,android,android-volley,Java,Android,Android Volley,在SearchFormFragment类中,有一个微调器,我想从json中获取数据。那么,如何在微调器中获取数据呢? 并给出在微调器中设置json数据的不同方法 FormType.java //POJO class public Class FormType { private String fid; private String formType; public String getFid() { return fid; } public void setFid(String f

在SearchFormFragment类中,有一个微调器,我想从json中获取数据。那么,如何在微调器中获取数据呢? 并给出在微调器中设置json数据的不同方法

FormType.java

//POJO class
public Class FormType {

private String fid;
private String formType;

public String getFid() {
    return fid;
}

public void setFid(String fid) {
    this.fid = fid;
}

public String getFormType() {
    return formType;
}

public void setFormType(String formType) {
    this.formType = formType;
}
}
SearchFormFragment.java

import android.app.DownloadManager;
import android.app.LoaderManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.ApplicationController;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.error.AuthFailureError;
import com.android.volley.error.VolleyError;
import com.android.volley.request.StringRequest;
import com.example.pc.formfillingapp.R;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import MainPages.Utils.Constants;
import MainPages.Utils.TinyDB;

/**
 * Created by PC on 29-12-2016.
 */

public class SearchFormFragment extends Fragment implements View.OnClickListener {

    ArrayList<FormType> formTypes = new ArrayList<FormType>();
    ArrayList<String> formName = new ArrayList<String>();

    Spinner spinner1;
    Toolbar toolbar;
    View view;
    FragmentActivity fragmentActivity;
    AppCompatActivity appCompatActivity;
    TinyDB tinyDB;

    public SearchFormFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.spinner, container, false);
        fragmentActivity = getActivity();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.v("onActivtyCreated", "inside it");
        super.onActivityCreated(savedInstanceState);
        tinyDB = new TinyDB(getActivity());

        //ToolBar for {NavigationView}
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);

        appCompatActivity = (AppCompatActivity) getActivity();
        appCompatActivity.setSupportActionBar(toolbar);
        appCompatActivity.getSupportActionBar().setTitle(R.string.app_name);

        spinner1 = (Spinner) view.findViewById(R.id.spinner_category_view1);

        getDataFromApi();
        Log.v("onAcitvityCreated", "endof it");
    }

    public void getDataFromApi() {
        MainActivity.showLoader(getActivity());
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
               "JSON URL", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Response", "response from type " + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    String message = jsonObject.getString("message");
                    JSONObject list = jsonObject.getJSONObject("list");
                    boolean status = jsonObject.getBoolean("status");
                    JSONArray data = list.getJSONArray("data");

                    if (status == true){
                        if (data != null) {
                            for (int i = 0; i < data.length(); i++) {

                                FormType form = new FormType();

                                JSONObject currentData = data.getJSONObject(i);
                                String fid = currentData.getString("fid");
                                String formType = currentData.getString("form_type");

                                form.setFid(fid);
                                form.setFormType(formType);
                                Log.e("FormType","type....");
                                formTypes.add(form);
                                formName.add(formType);
                                Log.e("Response","adding data...");
                            }
                            MainActivity.hideLoader();
                        }
                    }
                    spinner1.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, formName));
                    Log.e("Response","adding dataadapter");
                    MainActivity.hideLoader();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e("Response","exception");
                    MainActivity.hideLoader();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try {
                    String errorJson = String.valueOf(error.networkResponse);
                    JSONObject jsonObject = new JSONObject(errorJson);
                    String errorMessage = jsonObject.getString("error");

                    Toast.makeText(getContext(),errorMessage , Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), getResources().getString(R.string.exception) , Toast.LENGTH_LONG).show();
                }
                MainActivity.hideLoader();
            }
        }){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                return params;
            }
        };
        ApplicationController.getInstance().addToRequestQueue(stringRequest);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        Log.v("onClick", "inside");

        Log.v("onClick", "endof it");
    }
}
导入android.app.DownloadManager;
导入android.app.LoaderManager;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.design.widget.NavigationView;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.view.GravityCompat;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.LinearLayout;
导入android.widget.Spinner;
导入android.widget.Toast;
导入com.android.volley.ApplicationController;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.error.AuthFailureError;
导入com.android.volley.error.VolleyError;
导入com.android.volley.request.StringRequest;
导入com.example.pc.formfillingapp.R;
导入com.google.gson.JsonObject;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入MainPages.Utils.Constants;
导入MainPages.Utils.TinyDB;
/**
*PC于2016年12月29日创建。
*/
公共类SearchFormFragment扩展片段实现View.OnClickListener{
ArrayList formTypes=新的ArrayList();
ArrayList formName=新的ArrayList();
喷丝器喷丝器1;
工具栏;
视图;
碎片活动碎片活动;
AppCompatActivity AppCompatActivity;
TinyDB TinyDB;
公共SearchFormFragment(){
}
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.spinner,容器,假);
fragmentActivity=getActivity();
返回super.onCreateView(充气机、容器、savedInstanceState);
}
@凌驾
ActivityCreated上的公共无效(@Nullable Bundle savedinStateCState){
Log.v(“OnActivityCreated”、“inside”);
super.onActivityCreated(savedInstanceState);
tinyDB=新的tinyDB(getActivity());
//{NavigationView}的工具栏
toolbar=(toolbar)view.findviewbyd(R.id.toolbar);
appCompatActivity=(appCompatActivity)getActivity();
appCompatActivity.setSupportActionBar(工具栏);
appCompatActivity.getSupportActionBar().setTitle(R.string.app_name);
spinner1=(微调器)视图.findViewById(R.id.Spinner\u category\u视图1);
getDataFromApi();
Log.v(“onacitycreated”、“endof it”);
}
public void getDataFromApi(){
showLoader(getActivity());
StringRequest StringRequest=新StringRequest(Request.Method.POST,
“JSON URL”,新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.e(“响应”,“来自类型的响应”+响应);
试一试{
JSONObject JSONObject=新JSONObject(响应);
String message=jsonObject.getString(“消息”);
JSONObject list=JSONObject.getJSONObject(“list”);
boolean status=jsonObject.getBoolean(“status”);
JSONArray data=list.getJSONArray(“数据”);
如果(状态==真){
如果(数据!=null){
对于(int i=0;i<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/spinner_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="@dimen/activity_horizontal_margin">

        <TextView
            android:id="@+id/text_view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/simple_value"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:text="@string/spinner_category_type" />

        <Spinner
            android:id="@+id/spinner_category_view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/simple_value" />

    </LinearLayout>

</LinearLayout>