2017-06-21 36 views
0

我在微調設置頁面微調創造有醫院名稱來選擇 ,當在醫院選擇的醫院有一個電話號碼保存設置按鈕,點擊呼叫到醫院其他字符串陣列中存儲String數組

enter image description here

這是我的字符串數組

我有兩個字符串 - 數組首先是醫院
的電話號碼,第二個是我需要存儲號碼醫院名稱相同的線,當我在旋轉選定醫院名稱NER

<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> 

,這裏是我的活動:

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

下一次啓動應用程序時是否要使用保存的名稱和號碼? – sumit

+0

爲什麼不嘗試將醫院名稱和編號存儲到'.json'文件中,然後提取它們。 '[{「name」:「hospital 1」,「number」:「055270300」},{「name」:「hospital 2」,「number」:「055909000」}]' –

+0

#sumit我需要住院名字 #Nilesh Deokar你可以引導我或任何樣本 –

回答

1

我會建議使用Java創建,而不是XML數組。

它很簡單,

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

那麼醫院加入到這個數組列表是這樣的:

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

注意,這(醫院陣列)是你將在你的微調可以存儲陣列。

對於要實現的任務,這是怎麼了,我會繼續做它: 你可以多了一個ArrayList中存儲醫院和其各自的號碼用逗號分隔(,): 因此,創建另一個arrayist ,我們稱它爲數字。

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

現在,一旦用戶選擇了微調器上的項目,使用onItemSelected來獲取值並將其存儲在共享首選項中。

現在只需遍歷數字arrayList並收集數字,如果您選擇的值等於醫院名稱。您可以將逗號分隔的值分開。這樣:

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 
} 

} 

從這裏您可以在需要的任何活動中從您的共享首選項中撥打醫院號碼。

+0

Thx先生我會試試 –

+0

謝謝你的工作 –

0

您可以使用XML解析器拉爲了這個目的。 創建一個XML這樣

<?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> 

+0

可以準備任何與此和活動先生 –

+0

抱歉,我沒有得到你。 – gStephin

+0

好吧,但我現在知道我認爲在我的string.xml中創建新的 –

相關問題