Android 从其他字符串数组存储字符串数组

Android 从其他字符串数组存储字符串数组,android,Android,我在“设置”页面上使用“微调器”在“微调器”中创建医院名称以供选择 当选择医院时,在医院中有一个电话号码保存设置为按钮点击呼叫该医院 这是我的字符串数组 我有两个字符串-数组第一个是医院的电话号码 第二个是医院名称,当我在微调器中选择时,我需要将数字存储到医院名称中,和行相同 <string-array name ="number"> <item>055270300</item> <item>055909000</item&

我在“设置”页面上使用“微调器”在“微调器”中创建医院名称以供选择 当选择医院时,在医院中有一个电话号码保存设置为按钮点击呼叫该医院

这是我的字符串数组

我有两个字符串-数组第一个是医院的电话号码
第二个是医院名称,当我在微调器中选择时,我需要将数字存储到医院名称中,和行相同

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

055270300
055909000
055212222
第一医院
医院2
医院3
以下是我的活动:

    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();
        }
    });
}
TextView title=(TextView)findViewById(R.id.toolbar\u title);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
title.setText(toolbar.getTitle());
title.setText(“การแจ้งเหตุฉุกเฉิน");
phonenum=(EditText)findViewById(R.id.input\u phonenum);
message=(EditText)findViewById(R.id.put\u message);
preferences=getSharedPreferences(共享的首选项,Context.MODE\u PRIVATE);
editor=preferences.edit();
微调器=(微调器)findViewById(R.id.sos_微调器);
String[]hospital=getResources().getStringArray(R.array.hospital);
ArrayAdapter Adapter hospital=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,医院);
spinner.setAdapter(适配器医院);
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
putInt(String.valueOf(getResources().getStringArray(R.array.number)),R.array.hospital);
commit();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
按钮=(按钮)findViewById(R.id.save\u btn);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串getMessage=message.getText().toString();
int getPhonenum=Integer.parseInt(phonenum.toString());
putInt(存储的_phonenum,getPhonenum);
putString(存储的消息,getMessage);
commit();
Toast.makeText(HowtoActivity.this,“数据已保存”,
吐司。长度(短)。show();
}
});
}

为此,您可以使用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>

第一医院
71955555
第一医院
71955555
第一医院
71955555
第一医院
71955555

我建议使用java而不是xml创建数组

很简单,

ArrayList<String> hospitals= new ArrayList<>();
请注意,此(阵列)是将存储在微调器中的阵列

对于您想要完成的任务,我将这样做: 您可以再使用一个arraylist来存储hospital及其各自的编号,并用逗号(,)分隔: 因此,创建另一个阵列,让我们称之为数字

ArrayList<String> numbers = new ArrayList<>();
numbers.add("Hospital 1,71955555");
numbers.add("Hospital 2,71955556");
numbers.add("Hospital 3,71955557");
ArrayList number=new ArrayList();
编号。添加(“医院171955555”);
编号。添加(“医院271955556”);
编号。添加(“医院371955557”);
现在,一旦用户在微调器上选择了一个项目,就可以使用onItemSelected获取值并将其存储在共享首选项中

现在,只要在数字数组列表中循环并收集数字(如果您选择的值等于医院名称)。您可以在逗号处拆分该值。方法如下:

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
}

}

for(int i=0;i你想在下次启动应用程序时使用保存的姓名和号码吗?为什么不尝试将医院名称和号码存储到
.json
文件中,然后获取它们。
[{“姓名”:“医院1”,“号码”:“055270300”},{“姓名”:“医院2”,“号码”:“055909000”}]
#sumit我需要在医院名称中保留号码#Nilesh Deokar您能指导我或任何样本吗?可以用这个准备任何东西,在活动中Sir抱歉,我没有收到您的信息。!!好的,但我现在知道我想在我的字符串中创建新的。Xml请参考此示例——Thx先生,我尝试了Thx先生,我会尝试的
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
}

}