Reputation: 97
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
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();
}
});
}
Upvotes: 0
Views: 98
Reputation: 417
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.
Upvotes: 1
Reputation: 344
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>
Upvotes: 0