0

I create on Setting page With Spinner in spinner have a hospital name to select and when selected hospital in hospital have a phone number to save setting to button clicked call to that hospital

enter image description here

This my String-Array

I have two string - array first is a phone number of hospital
and second is hospital name I need to storing number to hospital name same as line when I selected in spinner

<string-array name ="number">
    <item>055270300</item>
    <item>055909000</item>
    <item>055212222</item>
</string-array>

<string-array name="hospital">
    <item>hospital 1</item>
    <item>hospital 2</item>
    <item>hospital 3</item>
</string-array>

And Here is my Activity:

    TextView title = (TextView) findViewById(R.id.toolbar_title);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    title.setText(toolbar.getTitle());
    title.setText("การแจ้งเหตุฉุกเฉิน");


    phonenum = (EditText)findViewById(R.id.input_phonenum);
    message = (EditText)findViewById(R.id.put_message);


    preferences = getSharedPreferences(shared_preferences,Context.MODE_PRIVATE);
    editor = preferences.edit();
    spinner = (Spinner)findViewById(R.id.sos_spinner);

    String[] hospital = getResources().getStringArray(R.array.hospital);
    ArrayAdapter<String> adapterhospital = new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1, hospital);
    spinner.setAdapter(adapterhospital);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            editor.putInt(String.valueOf(getResources().getStringArray(R.array.number)),   R.array.hospital);

            editor.commit();

        }

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

        }
    });

    button = (Button)findViewById(R.id.save_btn);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String getMessage = message.getText().toString();
            int getPhonenum = Integer.parseInt(phonenum.toString());

            editor.putInt(stored_phonenum, getPhonenum);
            editor.putString(stored_message , getMessage);
            editor.commit();

            Toast.makeText(HowtoActivity.this, "Data Saved.",
                    Toast.LENGTH_SHORT).show();
        }
    });
}
3
  • do you want to use the saved name and number next time you start your app?? Commented Jun 21, 2017 at 4:29
  • why don't you try storing hospital name and number into .json file and then fetching them. [{"name":"hospital 1","number":"055270300"},{"name":"hospital 2","number":"055909000"}] Commented Jun 21, 2017 at 4:30
  • #sumit i need keep number in hospital name #Nilesh Deokar Can you guide me or any sample Commented Jun 21, 2017 at 4:41

2 Answers 2

1

I would recommend creating the array using java as opposed to xml.

Its quite simple,

ArrayList<String> hospitals= new ArrayList<>();

then add the hospitals to this array list this way :

hospitals.add("Hospital 1");
hospitals.add("Hospital 2");
hospitals.add("Hospital 3");

Note that this(hospitals array) is the array you will be storing in your spinner.

For the task you want to achieve, this is how i would proceed to do it : You can have one more arraylist to store both hospital and its respective number seperated by a comma(,): So create another arrayist, lets call it numbers.

ArrayList<String> numbers = new ArrayList<>();
numbers.add("Hospital 1,71955555");
numbers.add("Hospital 2,71955556");
numbers.add("Hospital 3,71955557");

Now once the user selects an item on the spinner, use the onItemSelected to get the value and store it in shared preferences.

Now just loop through the number arrayList and collect the number if your selected value is equal to the hospital name. You can split the value where the comma is. This way :

for(int i=0;i<numbers.size();i++){

    String value = numbers.get(i);
    String names[]= value.split(",");
    String name = names[0];
    if(name.equalsIgnoreCase("Value stored from spinner"){
    String number = names[1];
    //Store this number in shared preferences as the value for the hospital //selected
}

}

From here you can call the hospital number from your shared preferences in any activity where it is needed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx Sir i Will try it
0

You can use xml pull Parser for this purpose. Create an xml like this

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<hospital>
    <hospital>
        <a_name>hospital 1</a_name>
        <a_number>71955555</a_number>
    </hospital>
   <hospital>
        <a_name>hospital 1</a_name>
        <a_number>71955555</a_number>
    </hospital>
    <hospital>
        <a_name>hospital 1</a_name>
        <a_number>71955555</a_number>
    </hospital>
    <hospital>
        <a_name>hospital 1</a_name>
        <a_number>71955555</a_number>
    </hospital>
</hospitalprovider>

5 Comments

Can prepare anything with this and in activity sir
sorry i didn't get you.!!
Ok but i know now i think create new in my string.xml
Thx Sir i try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.