android - Convert spinner value to string to use in php

475

Java file

public class CreateScreen extends AppCompatActivity {

    EditText e1,e2,e3,e4,e5,e6,e7;
    Button b1,b2;
    Spinner s1,s2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.activity_create_screen);
        final Intent i = getIntent();

        String vno = i.getStringExtra("vno");
        String com = i.getStringExtra("com");
        e1=(EditText)findViewById(R.id.editText8);
        e2=(EditText)findViewById(R.id.editText9);
        e3=(EditText)findViewById(R.id.editText10);
        e4=(EditText)findViewById(R.id.editText11);
        e5=(EditText)findViewById(R.id.editText12);
        e6=(EditText)findViewById(R.id.editText13);
        e7=(EditText)findViewById(R.id.editText14);

        e1.setText(vno);
        e2.setText(com);
        s1=(Spinner)findViewById(R.id.spinner);
        String[] items = new String[]{"RAW MATERIAL", "FINISHED MATERIAL", "SEMI FINISHED MATERIAL"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
        s1.setAdapter(adapter);
        final   String matgrp = adapter.toString();

        s2=(Spinner)findViewById(R.id.spinner2);
        String[] item = new String[]{"INR", "USD", "MYR","JPY", "SGD"};

        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, item);
        s2.setAdapter(adapter1);

        final String currency = adapter1.toString();
        b1=(Button)findViewById(R.id.save);
        b2=(Button)findViewById(R.id.home);

        b2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(CreateScreen.this,MaterialMenu.class);
                startActivity(i);
            }
        });
        b1.setOnClickListener(new View.OnClickListener() {
            InputStream is =null;
            String result1 =null;

            @Override
            public void onClick(View v) {
                String a = e1.getText().toString();
                String b = e2.getText().toString();
                String c = e3.getText().toString();
                String d = e4.getText().toString();
                String z = e5.getText().toString();
                String f = e6.getText().toString();
                String g = e7.getText().toString();

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("materialnumber", a));
                nameValuePairs.add(new BasicNameValuePair("companycode", b));
                nameValuePairs.add(new BasicNameValuePair("desc", c));
                nameValuePairs.add(new BasicNameValuePair("mattype", matgrp));

                nameValuePairs.add(new BasicNameValuePair("matgroup", d));
                nameValuePairs.add(new BasicNameValuePair("baseunit", z));
                nameValuePairs.add(new BasicNameValuePair("priceunit", f));
                nameValuePairs.add(new BasicNameValuePair("orderunit", g));
                nameValuePairs.add(new BasicNameValuePair("curr",currency));

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://10.0.2.2/android/create.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpClient.execute(httpPost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    android.util.Log.e("log_tag", "connection success ");

                } catch (Exception e) {
                    android.util.Log.e("log_tag", "Error in http connection " + e.toString());
                    Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
                }
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();

                    result1 = sb.toString();


                } catch (Exception e) {
                    android.util.Log.e("log_tag", "Error converting result " + e.toString());
                }
                try
                {
                    JSONObject object = new JSONObject(result1);
                    String w = object.getString("re");
                    if(w.equals("Material Created successfully"))
                    {
                        Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(CreateScreen.this, Menu.class);

                        startActivity(i);
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                    }
                }
                catch (Exception e)
                {
                    Log.e("log_tag", "Error parsing data "+e.toString());
                    Toast.makeText(getApplicationContext(), "JsonArray failed to load", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

logcat

value cannot be converted to string

How do I need to rewrite this code to convert the spinner value to a string to be used in JsonObject and in PHP?

909

Answer

Solution:

In your Adapter class.

Ex :

public class AmenityListAdapter extends SomeAdapter(BaseAdapter, ArrayAdapter etc etc) {

@Override
public String toString() {
   StringBuffer strBuf = "";
   for(String myarr:Items) {
       strBuf.concat(myarr+",");
   }
   return strBuf.toString;
}
}
957

Answer

Solution:

Here:

final String currency = adapter1.toString();

In this line toString() method return string representation ofadapter1ArrayAdapter object instead of selected value from Spinner.

Get selected value fromSpinner as:

nameValuePairs.add(new BasicNameValuePair("curr",s2.getSelectedItem().toString()));

do same for getting value from Spinners1.

387

Answer

Solution:

Override toString method inside adapter class

@Override
public String toString() {
   return super.toString();
}
777

Answer

Solution:

remove these code.

final String matgrp = adapter.toString(); final String currency = adapter1.toString();

and use setOnItemClickListner() method for spinner there you can get parent variable by using the variable you can get the string using parent.getItemAtPosition().

People are also looking for solutions to the problem: php - problems searching database to match user input

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.