Raahul Raahul - 6 months ago 26
Android Question

Access Spinner value globally

string.xml file

<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();
}

}
});
}
}


Only the first item "Raw Material"is getting stored into mysql database,even if i choose another items. How to access the selected value to be stored to mysql database.

Answer

in Spiner we have 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) {

                }
            });
Comments