簡體   English   中英

全局訪問微調器值

[英]Access Spinner value globally

string.xml文件

<resources>
    <string name="app_name">MM</string>
    <string-array name="material_type">
        <item>Raw Material</item>
        <item>Finished Material</item>
        <item>Semi-finished Material</item>
    </string-array>
</resources>

mainactivity.java

Spinner s1=(Spinner)findViewById(R.id.spinner);

  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.material_type, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s1.setAdapter(adapter);
       final String materialType = s1.getSelectedItem().toString();
  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();

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("mat", a));
                nameValuePairs.add(new BasicNameValuePair("comp", b));
                nameValuePairs.add(new BasicNameValuePair("desc", c));
                nameValuePairs.add(new BasicNameValuePair("mattype", materialType));


                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://10.0.2.2/android/matmas/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 Details Created successfully"))
                  {
                      Toast.makeText(getApplicationContext(), w, Toast.LENGTH_SHORT).show();
                      Intent i = new Intent(getApplicationContext(),MenuScreen.class);
                      startActivity(i);

                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(), "FILL REQUIRED FIELDS", Toast.LENGTH_SHORT).show();
                      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();
              }

            }
        });
    }
}

即使我選擇其他項目,也只有第一個項目“ Raw Material”被存儲到mysql數據庫中。 如何訪問所選值以存儲到mysql數據庫中。

您使用Set Onclick偵聽器,您需要在下面的界面中使用任何項目單擊或微調器值更改

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new  AdapterView.OnItemSelectedListener (){

  public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
});

Spiner我們有setOnItemSelectedListener

        String temp [];
        Spinner s1=null;
        String materialType;

        temp = getResources().getStringArray(R.array.material_type);
            materialType = s1.getSelectedItem().toString();
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
                    R.array.material_type, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s1.setAdapter(adapter);


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

                }

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

                }
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM