将微调器值转换为要在php中使用的字符串

时间:2016-05-20 08:57:06

标签: php android json

Java文件

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的

  

值无法转换为字符串

如何重写此代码以将微调器值转换为要在JsonObject和PHP中使用的字符串?

4 个答案:

答案 0 :(得分:0)

下面:

  

final String currency = adapter1.toString();

在此行toString()方法中返回adapter1 ArrayAdapter对象的字符串表示形式,而不是Spinner中的选定值。

Spinner获取选定的值:

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

从Spinner s1获取价值也是如此。

答案 1 :(得分:0)

覆盖适配器类

中的toString方法
@Override
public String toString() {
   return super.toString();
}

答案 2 :(得分:0)

在适配器类中。

前:

Linear Layout

答案 3 :(得分:0)

删除这些代码。

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

并为spinner使用setOnItemClickListner()方法,您可以使用变量获取父变量,您可以使用parent.getItemAtPosition()获取字符串。