dongmu5596 2016-05-20 08:57
浏览 72
已采纳

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

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 + "
");
                    }
                    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?

  • 写回答

4条回答 默认 最新

  • drc4925 2016-05-20 13:34
    关注

    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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler