简体   繁体   中英

how to send spinner data by volley to database

how to send spinner data by volley to database?

The codes are all sent and no problem Only Spinner field is not sent to the data table.

String[] Cat = {"املاک","وسایل نقلیه","لوازم الکترونیکی","مربوط به خانه","خدمات","وسایل شخصی","سرگرمی و فراغت","اجتماعی","برای کسب و کار","استخدام و کاریابی"};

String[] CatCode = {"1","2","3","4","5","6","7","8","9","10"};

String cate = "";

Send Data Class :

String name,description,phone,email,city;

String[] Cat = {"املاک","وسایل نقلیه","لوازم الکترونیکی","مربوط به خانه","خدمات","وسایل شخصی","سرگرمی و فراغت","اجتماعی","برای کسب و کار","استخدام و کاریابی"};

String[] CatCode = {"1","2","3","4","5","6","7","8","9","10"};

String cate = "";

StringRequest AddAdvReq;
private String Post_Url = "http://192.168.1.102/tablo/api/get_new_adv"
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_adv);

    defineVolleyCodes();
    defineViews();
    defineClicks();

    ArrayAdapter<String> catAdapter = new ArrayAdapter<>(getApplicationContext(),R.layout.spinner_row,Cat);
    SpinCategory.setAdapter(catAdapter);
    SpinCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            cate = CatCode[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            cate = CatCode[0];
        }
    });

}
//==========================
private void defineVolleyCodes() {

    AddAdvReq = new StringRequest(Request.Method.POST, Post_Url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Toast.makeText(AddAdv.this, response, Toast.LENGTH_SHORT).show();
                    finish();

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Toast.makeText(AddAdv.this, error.toString(), Toast.LENGTH_SHORT).show();

                }
            }
    )
    {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            //Add Post Parameters
            String STRImage = getStringImage(bitmap);
            params.put("name",name);
            params.put("description",description);
            params.put("phone",phone);
            params.put("email",email);
            params.put("city",city);
            params.put("category_id",cate);

            return params;
        }
    };

}
//==========================
private void defineViews() {

    //===========================Define All EditTexts

    ETxtName = (EditText)findViewById(R.id.etxt_name);
    ETxtDescription = (EditText)findViewById(R.id.etxt_description);
    ETxtPhone = (EditText)findViewById(R.id.etxt_phone);
    ETxtEmail = (EditText)findViewById(R.id.etxt_email);
    ETxtCity = (EditText)findViewById(R.id.etxt_city);

    //==============================Define All Buttons

    SpinCategory = (Spinner)findViewById(R.id.spin_category);

    //==============================Define All Buttons

    BtnAddAdv = (Button)findViewById(R.id.btn_add_adv);

}
//==========================
private void defineClicks() {

    BtnAddAdv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Load Fields

            name = ETxtName.getText().toString();
            description = ETxtDescription.getText().toString();
            phone = ETxtPhone.getText().toString();
            email = ETxtEmail.getText().toString();
            city = ETxtCity.getText().toString();
            cate = SpinCategory.getSelectedItem().toString();

            //Add Request To Queue
            AppController.getInstance().addToRequestQueue(AddAdvReq);
        }
    });

}

}

params.put("category_id",cate); cate = SpinCategory.getSelectedItem().toString();

What's the problem? Thanks for all.

You are storing the selected item in the variable here

SpinCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            cate = CatCode[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            cate = CatCode[0];
        }
    });

cate - use it. In the click listener you override it with the new value from the spinner that may be invalid(I cannot tell you more because I don't see the whole code)

just delete this cate = SpinCategory.getSelectedItem().toString(); line from the BtnAddAdv click listener and try - maybe it will work

Hope it helps.

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

    defineVolleyCodes(); // Remove heare
    defineViews();
    defineClicks(); // Remove heare

    ArrayAdapter<String> catAdapter = new ArrayAdapter<>(getApplicationContext(),R.layout.spinner_row,Cat);
    SpinCategory.setAdapter(catAdapter);

    SpinCategory.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            cate = parent.getItemAtPosition().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });   
    defineVolleyCodes();
    defineClicks();
}

please set this method call after the SpinCategory as above code mention.

Then in BtnAddAdv cate= cate; in your code.

I hope it'll help you...!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM