Spinner没有在UI中显示任何值,但如果单击它,则值就在那里

时间:2018-02-03 05:36:01

标签: android jquery

Spinner在UI中没有显示任何值,但如果单击它,则值就在那里。但是,让我告诉你,它不是颜色。 此外,当我尝试获取所选项时,它返回null。 我试图用#34; spesialis"填充我的微调器。我从json得到的,以及我从数据库(sql)使用volley获得的json。 我的代码中没有错误,但是没有在UI中显示任何内容,但是当我单击微调器时,它会显示值,但是当我单击该值时,它们将返回null而不是

这是我的代码:

private void loadSpinnerData(){
        final TextView s=findViewById(R.id.txtx);
        final ArrayList<String> isi = new ArrayList<String>();
        String url = Variables.url;
        RequestQueue rq2 = Volley.newRequestQueue(this);
        StringRequest sq2 = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            System.out.println("response "+response);
                            JSONObject jo= null;
                            try {
                                jo = new JSONObject(response);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String sukses=jo.getString("sukses");
                            System.out.println("Kamu :"+sukses);
                            if (sukses.equals("1")){
                                JSONArray ja= jo.getJSONArray("spesialis");
                                for (int i=0;i<ja.length(); i++){
                                                 JSONObject jojo=ja.getJSONObject(i);
                                    String dataspinner = jojo.getString("spesialis").trim();
                                    isi.add(dataspinner);
                                }
                            }
                        }
                        catch (JSONException e){
                            Toast.makeText(signUp.this, ""+e, Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    public void onErrorResponse(VolleyError error) {
                        Toast salah = Toast.makeText(getApplicationContext(),
                                "Something went wrong", Toast.LENGTH_SHORT);
                        salah.show();
                    }
                }
        )
        {
            protected Map<String, String> getParams(){
                Map<String, String> params = new HashMap<String, String>();
                params.put("action", "17");
                return params;
            }
        } ;
        rq2.add(sq2);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, isi);
        adapter.notifyDataSetChanged();
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner sItems = findViewById(R.id.spSpesialis);
        sItems.setAdapter(adapter);

我希望你们能帮助我,谢谢 编辑:

这是我对截击的反应:

response {"spesialis":[{"id_spesialis":"1","spesialis":"NONE"},{"id_spesialis":"2","spesialis":"Spesialis Tulang"},{"id_spesialis":"3","spesialis":"Genetial"}],"sukses":"1"}

1 个答案:

答案 0 :(得分:0)

试试这个

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    String[] SpnID, SpnName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = findViewById(R.id.spn);

        ArrayList<POJO> pojoArrayList = new ArrayList<>();

        String response = " {\n" +
                " \t\"spesialis\": [{\n" +
                " \t\t\"id_spesialis\": \"1\",\n" +
                " \t\t\"spesialis\": \"NONE\"\n" +
                " \t}, {\n" +
                " \t\t\"id_spesialis\": \"2\",\n" +
                " \t\t\"spesialis\": \"Spesialis Tulang\"\n" +
                " \t}, {\n" +
                " \t\t\"id_spesialis\": \"3\",\n" +
                " \t\t\"spesialis\": \"Genetial\"\n" +
                " \t}],\n" +
                " \t\"sukses\": \"1\"\n" +
                " }";

        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.optJSONArray("spesialis");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                POJO pojo = new POJO();
                pojo.setName(object.getString("spesialis"));
                pojo.setId(object.getString("id_spesialis"));
                pojoArrayList.add(pojo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        SpnName = new String[pojoArrayList.size()];
        SpnID = new String[pojoArrayList.size()];

        for (int i = 0; i < pojoArrayList.size(); i++) {

            SpnName[i] = pojoArrayList.get(i).getName();
            SpnID[i] = pojoArrayList.get(i).getId();
        }

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, SpnName);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(arrayAdapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(MainActivity.this, "Clicked item ID  :-> " + SpnID[i], Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, "Clicked item Name :-> " + SpnID[i], Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}

POJO CLASS

public class POJO
{
    String id,name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}