为什么我的队列无法运行?以及如何使用改造2从数据库中检索微调器数据

时间:2018-07-31 04:20:42

标签: android retrofit2

我想使用改造2从数据库中检索微调器数据,但是当我运行程序时,我的呼叫入队将不会运行(我知道因为progressdialog不会关闭)

这是我的代码

public class MainActivity extends AppCompatActivity {

    public static final String URL = "http://192.168.43.234/";
    private ProgressDialog progress;
    @BindView(R.id.spinner2)
    Spinner spinnerDosen;
    ProgressDialog loading;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = this;

        initSpinnerDosen();

        spinnerDosen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedName = parent.getItemAtPosition(position).toString();

                Toast.makeText(mContext, "Kamu memilih dosen " + selectedName, Toast.LENGTH_SHORT).show();
            }

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

            }
        });
    }

    public void initSpinnerDosen(){
     loading = ProgressDialog.show(mContext, null, "harap tunggu...", true, false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RegisterAPI api = retrofit.create(RegisterAPI.class);
        Call<Value> call = api.getData();
        Log.d("CALL1", "BISA");
        call.enqueue(new Callback<Value>() {
            @Override
            public void onResponse(Call<Value> call, Response<Value> response) {
                if (response.isSuccessful()) {
                    loading.dismiss();
                    List<Value> ValueItem = response.body().getResult();
                    List<String> listSpinner = new ArrayList<String>();

                    for (int i = 0; i < ValueItem.size(); i++){
                        String name = ValueItem.get(i).getProduct_description();
                        listSpinner.add(name);
                    }
                    // Set hasil result json ke dalam adapter spinner
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.activity_list_item, listSpinner);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinnerDosen.setAdapter(adapter);
                } else {
                    loading.dismiss();
                    Toast.makeText(mContext, "Gagal mengambil data dosen", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Value> call, Throwable t) {

            }
        });}
        }

我还想问一下我的getResult()是否正确? 这里的代码

public class Value {

    @SerializedName("product_description")
    private String product_description;

    public String getProduct_description() {
        return product_description;
    }

    @SerializedName("result")
    private List result;
    public List<Value> getResult() {
        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

只是改变

public static final String URL = "http://192.168.43.234/";
private ProgressDialog progress;
@BindView(R.id.spinner2)
Spinner spinnerDosen;
ProgressDialog loading;
Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    mContext = this;

    initSpinnerDosen();

    spinnerDosen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String selectedName = parent.getItemAtPosition(position).toString();

        Toast.makeText(mContext, "Kamu memilih dosen " + selectedName, Toast.LENGTH_SHORT).show();
    }

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

    }
});
}

public void initSpinnerDosen(){
    loading = ProgressDialog.show(mContext, null, "harap tunggu...", true, false);
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();
    RegisterAPI api = retrofit.create(RegisterAPI.class);
    Call<Value> call = api.getData();
   Log.d("CALL1", "BISA");
   call.enqueue(new Callback<Value>() {
    @Override
    public void onResponse(Call<Value> call, Response<Value> response) {
        if (response.isSuccessful()) {
            loading.dismiss();
            List<Value> ValueItem = response.body().getResult();
            List<String> listSpinner = new ArrayList<String>();

            for (int i = 0; i < ValueItem.size(); i++){
                String name = ValueItem.get(i).getProduct_description();
                listSpinner.add(name);
            }
            // Set hasil result json ke dalam adapter spinner
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.activity_list_item, listSpinner);

      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerDosen.setAdapter(adapter);
            loading.dismiss();  //here dismiss loading also
        } else {
            loading.dismiss();
            Toast.makeText(mContext, "Gagal mengambil data dosen", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<Value> call, Throwable t) {
        loading.dismiss();  //here dismiss loading also
    }
});}
}