Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.priem_ali.priem_patient;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ArrayAdapter;
- import android.widget.ShareActionProvider;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Created by Priem-Ali on 9/22/2016.
- */
- public class spinnerActivity extends Activity {
- private Spinner spinner;
- private TextView textView12;
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spineractivity);
- spinner = (Spinner)findViewById( R.id.spinner );
- textView12 = (TextView)findViewById( R.id.textView12 );
- String strJson="{\"products\":[{\"item_id\":\"103\",\"name\":\"Crispy Broast\",\"category\":\"Broast\",\"unit_price\":\"180.00\",\"cost_price\":\"180.00\",\"quantity\":\"100\",\"tax\":\"10.00\"}]}";
- String data = "";
- try {
- JSONObject jsonRootObject = new JSONObject(strJson);
- //Get the instance of JSONArray that contains JSONObjects
- JSONArray jsonArray = jsonRootObject.optJSONArray("products");
- // Creating adapter for spinner
- //Iterate the jsonArray and print the info of JSONObjects
- for(int i=0; i < jsonArray.length(); i++){
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- String name = jsonObject.optString("name").toString();
- String category = jsonObject.optString("category").toString();
- String unitp = jsonObject.optString("unit_price").toString();
- String quan = jsonObject.optString("quantity").toString();
- //data += " \n Name= "+ name +" \n , \n" + " category= \"+ category +\" \n , \n" + "\" + \" unitprice= \"+ unitp +\" \n + \"\" + \" quanantity= \"+ quan +\" \n "
- ;
- data += name + "\n"+ category + "\n" + unitp + "\n"+quan;
- }
- try {
- String arra [] = data.split("\n");
- ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.expandable_list_content, arra);
- // Drop down layout style - list view with radio button
- dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- // attaching data adapter to spinner
- spinner.setAdapter(dataAdapter);
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(this , e.toString() , Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- Toast.makeText(this , e.toString() , Toast.LENGTH_LONG).show();
- textView12.setText(e.toString()+data+"Google");
- e.printStackTrace();}
- textView12.setText(data+"\n");
- }
- private ShareActionProvider mShareActionProvider;
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate menu resource file.
- getMenuInflater().inflate(R.menu.menu, menu);
- // Locate MenuItem with ShareActionProvider
- MenuItem item = menu.findItem(R.id.menu_item_share);
- // Fetch and store ShareActionProvider
- mShareActionProvider = (ShareActionProvider) item.getActionProvider();
- // Return true to display menu
- return true;
- }
- // Call to update the share intent
- private void setShareIntent(Intent shareIntent) {
- if (mShareActionProvider != null) {
- mShareActionProvider.setShareIntent(shareIntent);
- }
- }
- }
Add Comment
Please, Sign In to add comment