如何从res string.xml调用字符串?

时间:2016-04-27 07:08:43

标签: android xml

这是我的string.xml。我想在xml中调用字符串名称url。我该怎么称呼它?

$("input").keypress(function(){
    $("span").text(i += 1);
    alert("Test key pres");
});

<input type="text">

<p>Keypresses: <span>0</span></p>

这就是我打电话的方式。但是我一直在收到错误。我怎么称呼它?

private void showconfigurableProducts() {
            try {
                    count = 0;
                    mllSpinner.setVisibility(View.VISIBLE);
                    for (int i = 0; i < lisProductDetail.get(0).getConfigurable()
                                    .size(); i++) {
                            TextView tv = new TextView(getActivity());
                            tv.setText(lisProductDetail.get(0).getConfigurable().get(i)
                                            .getLabel());
                            mllSpinner.addView(tv);
                            final Spinner spinner = new Spinner(getActivity());
                            spinner.setTag(i);
                            final ArrayAdapter<OptionProductDetailModel> arrayAdapter=new ArrayAdapter<OptionProductDetailModel>(
                                            getActivity(),
                                            android.R.layout.simple_spinner_dropdown_item,
                                            lisProductDetail.get(0).getConfigurable().get(i)
                                                            .getOptions());
                            spinner.setAdapter(arrayAdapter);
                            spinner.setSelection(0, false);
                            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                                    private String attributeId;

                                    @Override
                                    public void onItemSelected(AdapterView<?> parent,
                                                    View view, int position, long id) {
                                            OptionProductDetailModel option = (OptionProductDetailModel) spinner
                                                            .getSelectedItem();
                                            List<OptionProductDetailModel> sizedataArray = new ArrayList<OptionProductDetailModel>();
                                            sizedataArray.add(option);
                                            if (listDummy.size() > 0) {
                                                    selectbtatrue = check(sizedataArray, listDummy);
                                            }

                                            if (selectbtatrue) {
                                                    return;
                                            } else {

                                                    for (int i = 0; i < lisProductDetail.get(0)
                                                                    .getConfigurable().size(); i++) {
                                                            for (int j = 0; j < lisProductDetail.get(0)
                                                                            .getConfigurable().get(i).getOptions()
                                                                            .size(); j++) {
                                                                    if (lisProductDetail.get(0)
                                                                                    .getConfigurable().get(i)
                                                                                    .getOptions().get(j).getLabel() == option
                                                                                    .getLabel()) {
                                                                            attributeId = lisProductDetail.get(0)
                                                                                            .getConfigurable().get(i)
                                                                                            .getId();
                                                                    }
                                                            }
                                                    }
                                                    for (int i = 0; i < lisProductDetail.get(0)
                                                                    .getConfigurable().size(); i++) {
                                                            if (attributeId != lisProductDetail.get(0)
                                                                            .getConfigurable().get(i).getId()) {
                                                                    // if (listDummy.size() > 0) {
                                                                    // listDummy = new
                                                                    // ArrayList<OptionProductDetailModel>();
                                                                    // }
                                                                    for (int k = 0; k < option.getProducts()
                                                                                    .size(); k++) {
                                                                            for (int j = 0; j < lisProductDetail
                                                                                            .get(0).getConfigurable()
                                                                                            .get(i).getOptions().size(); j++) {
                                                                                    for (int l = 0; l < lisProductDetail
                                                                                                    .get(0).getConfigurable()
                                                                                                    .get(i).getOptions().get(j)
                                                                                                    .getProducts().size(); l++) {
                                                                                            if (option
                                                                                                            .getProducts()
                                                                                                            .get(k)
                                                                                                            .equals(lisProductDetail
                                                                                                                            .get(0)
                                                                                                                            .getConfigurable()
                                                                                                                            .get(i)
                                                                                                                            .getOptions()
                                                                                                                            .get(j)
                                                                                                                            .getProducts()
                                                                                                                            .get(l))) {

                                                                                                    listDummy
                                                                                                                    .add(lisProductDetail
                                                                                                                                    .get(0)
                                                                                                                                    .getConfigurable()
                                                                                                                                    .get(i)
                                                                                                                                    .getOptions()
                                                                                                                                    .get(j));
                                                                                            } else {

                                                                                            }
                                                                                    }
                                                                            }
                                                                            System.out.print(listDummy);
                                                                    }

                                                            }
                                                            if (count < 2) {
                                                                    int tag = (int) spinner.getTag();
                                                                    if (tag == i) {

                                                                    } else {
                                                                            lisProductDetail.get(0)
                                                                                            .getConfigurable().get(i)
                                                                                            .setOptions(listDummy);  // Here i am getting S,M,L but  i do not know how to update in in Size Attribute Spinner. 

                                                                    }
                                                                    count = count + 1;
                                                            }
                                                    }
                                            }

                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {
                                            // TODO Auto-generated method stub

                                    }

                            });
                            mllSpinner.addView(spinner);
                    }

            } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
    }


 private boolean check(List<OptionProductDetailModel> SelectedArray,
            List<OptionProductDetailModel> DummyArray) {
        for (int i = 0; i < SelectedArray.size(); i++) {
            for (int j = 0; j < DummyArray.size(); j++) {
                if (SelectedArray.get(i).getId()
                        .equalsIgnoreCase(DummyArray.get(j).getId())) {
                    btatrue = true;
                } else {
                    btatrue = false;
                }
            }
        }
        return btatrue;
    }

6 个答案:

答案 0 :(得分:1)

将此代码用于您的工作

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);

答案 1 :(得分:0)

你应该使用

来调用它
String ipAddress = getApplicationContext().getResources().getString(R.string.url);

请参阅相关文档here

答案 2 :(得分:0)

要了解详情:http://developer.android.com/guide/topics/resources/string-resource.html

String url= getApplicationContext().getResources().getString(R.string.url);

答案 3 :(得分:0)

试试这个

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);

答案 4 :(得分:0)

试试这样....

 String ipAddress =getApplicationContext().getResources().getString(R.string.url);

以及更多信息: - http://developer.android.com/guide/topics/resources/string-resource.html

答案 5 :(得分:-1)

在你的问题中。你在xml中说过但是

String ipAddress = getApplicationContext().getString(R.strings.url);

是一个java代码。在xml中你可以像这样做

 <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="@string/url" />

此外,在您的错误中,您应该在getString方法之前访问您的资源

String ipAddress = getApplicationContext().getResources().getString(R.strings.url);