如果字符串找不到字符串

时间:2016-09-27 12:47:23

标签: android string

如果动态xml中不存在空字符串,是否可以创建空字符串。

例如以下代码: -

super.onCreate(savedInstanceState);
    Bundle bundle = getIntent().getExtras();
    String SManuf = bundle.getString("SManuf");
    String manuf = bundle.getString("manuf");
    String panel = bundle.getString("panel");
    setTitle("Instructions for "+manuf+" "+panel);


    String Ins = (SManuf + panel+"Noof");
    String InsI = (SManuf + panel);

   // Toast.makeText(getApplicationContext(), Ins, Toast.LENGTH_SHORT).show();


    int resId = getResources().getIdentifier(Ins, "string", getPackageName());
    String InsNo = getResources().getString(resId);


    scrollviewTerms = new ScrollView(this);
    LinearLayout linearlayout = new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    scrollviewTerms.addView(linearlayout);
   //for (int i = 0; i < 13; i++)
    int foo = Integer.parseInt(InsNo);
    for (int i = 0; i < foo; i++)
    {

        LinearLayout linearManufTB = new LinearLayout(this);
        linearManufTB.setOrientation(LinearLayout.HORIZONTAL);
        linearlayout.addView(linearManufTB);
        Termsb = new Button(this);
        int id = getResources().getIdentifier("PanMansTitle" + i, "string", getPackageName());
        String Terms = getResources().getString(id);
       // Toast.makeText(getApplicationContext(), Terms, Toast.LENGTH_SHORT).show();
        Termsb.setText(Terms);
        Termsb.setId(i);
        Termsb.setTextSize(30);
        Termsb.setPadding(0, 0, 0, 0);

        LinearLayout linearManufTV = new LinearLayout(this);
        linearManufTV.setOrientation(LinearLayout.HORIZONTAL);
        linearlayout.addView(linearManufTV);

        String TermsS = Terms.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&amp;",""));

        TermstINS = new TextView(this);
        int ids = getResources().getIdentifier(InsI + TermsS, "string", getPackageName());

        String TermsTV = getResources().getString(ids);
        TermstINS.setText(TermsTV);
        TermstINS.setId(i);
        TermstINS.setTextSize(20);
        TermstINS.setBackgroundColor(Color.parseColor("#999999"));
        TermstINS.setPadding(0, 0, 0, 0);

        LinearLayout linearManufTV2 = new LinearLayout(this);
        linearManufTV2.setOrientation(LinearLayout.HORIZONTAL);
        linearlayout.addView(linearManufTV2);

        TermstNOTS = new TextView(this);
        int idn = getResources().getIdentifier(InsI + TermsS +"N", "string", getPackageName());

        String TermsTVN = getResources().getString(idn);

        TermstNOTS.setText(TermsTVN);
        TermstNOTS.setId(i);
        TermstNOTS.setTextSize(20);
        TermstNOTS.setBackgroundColor(Color.parseColor("#515151"));
        TermstNOTS.setPadding(0, 0, 0, 0);

        // b.setTypeface(Typeface.SERIF,Typeface.ITALIC);



        Termsb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        TermstINS.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        TermstNOTS.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearManufTB.addView(Termsb);
        linearManufTV.addView(TermstINS);
        linearManufTV2.addView(TermstNOTS);

        if(TermstINS.length()== 0){ linearManufTB.setVisibility(View.GONE);linearManufTV.setVisibility(View.GONE);linearManufTV2.setVisibility(View.GONE);}else{
            linearManufTB.setVisibility(View.VISIBLE);   linearManufTV.setVisibility(View.GONE);  linearManufTV2.setVisibility(View.GONE);}

列出了下面的标题和说明,以及下面的注释。

然而,标题列表固定为title0到标题200+,但并非所有系统都具有相同的标题,因为它们没有相同的功能,但会共享其中一些。

而不是输入200多条xml文件中空白的说明和注释,是否可以说该项目当前不可用,即时生成空白。

因此使其表现得更聪明,并且不需要输入大量死数据。

目前任何没有说明的东西都不会显示标题(不是空的),或说明和注释,但是,这要求我在没有任何指令和注释的情况下为xml文件指令和注释创建数据条目

我确信这是基本的,但我找不到任何可以交叉引用的东西。任何关于如何解决它的合适例子或想法的帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:3)

getResources().getIdentifier(String name, String defType, String defPackage)返回资源的id。如果资源不存在,它将返回0

您可以像这样更改代码:

String defaultString = "";
String Terms = id != 0 ? getResources().getString(id) : defaultString;

答案 1 :(得分:0)

String your_string = ""; 

这给了一个空的&#34;你的字符串的值。