如何根据字符串值

时间:2016-01-26 19:51:18

标签: android list sorting button alphabetical

<string name="Manuf0">best</string>
<string name="Manuf1">Bravo</string>
<string name="Manuf2">zoo</string>
<string name="Manuf3">Skitz</string>
<string name="Manuf4">don</string>
<string name="Manuf5">animal</string>

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    scrollviewManuf = new ScrollView(this);
    LinearLayout linearlayout = new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    scrollviewManuf.addView(linearlayout);
    for (int i = 0; i < 5; i++)

    {

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

        Manufb = new Button(this);

        int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
        String Manuf = getResources().getString(id);
        Manufb.setText(Manuf);
        Manufb.setId(i);
        Manufb.setTextSize(30);
        Manufb.setPadding(0, 0, 0, 0);

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

        Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearManuf.addView(Manufb);


        Manufb.setOnClickListener(new View.OnClickListener() {


            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                String SManuf= Manuf.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&amp;",""));
               //Panel= getResources().getString(id);
                Toast.makeText(getApplicationContext(), SManuf , Toast.LENGTH_SHORT).show();

                Intent passIntent = new Intent(Manufacturers.this,panels.class);
                passIntent.putExtra("SManuf",SManuf);
                startActivity(passIntent);
            }

        });

    }

    this.setContentView(scrollviewManuf);
}
}

如何根据字符串值按字母顺序对从以下代码生成的按钮进行排序。 目前它们被列为按钮生成0到5。

列表在xml字符串文件中,想按字母顺序排列,所以我可以根据需要添加更多文件,编程只是按字母顺序对它进行排序。

还没有找到任何东西,但我猜我可能需要在文件中定义列表并对该列表进行排序,任何人都可以指出我正确的方向。

好的,所以我可以看到代码正在执行某些操作,但排序顺序没有改变,并且字符串s在intellij中显示为未使用,:下面的新代码: -

使用// here

标记了部分
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    ArrayList<String> theStrings = new ArrayList<>();//Here
    scrollviewManuf = new ScrollView(this);
    LinearLayout linearlayout = new LinearLayout(this);
    linearlayout.setOrientation(LinearLayout.VERTICAL);
    scrollviewManuf.addView(linearlayout);

    for (int i = 0; i < 28; i++) {

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

        Manufb = new Button(this);

        int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
        String Manuf = getResources().getString(id);
        theStrings.add(Manuf); /// Here
        Manufb.setText(Manuf);
        Manufb.setId(i);
        Manufb.setTextSize(30);
        Manufb.setPadding(0, 0, 0, 0);

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

        Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        linearManuf.addView(Manufb);


        Manufb.setOnClickListener(new View.OnClickListener() {


            @Override

            public void onClick(View v) {
                String PManuf =Manuf;
                // TODO Auto-generated method stub

                String SManuf= Manuf.replaceAll("&","").replaceAll(" ","").replaceAll("/","").replaceAll(" / ","").replaceAll("/ ","").replaceAll(" /","".replaceAll("&amp;",""));
               //Panel= getResources().getString(id);
                Toast.makeText(getApplicationContext(), Manuf+" Selected" , Toast.LENGTH_SHORT).show();

                Intent passIntent = new Intent(Manufacturers.this,panels.class);
                passIntent.putExtra("SManuf",SManuf);
                passIntent.putExtra("PManuf",PManuf);
                startActivity(passIntent);
            }

        } );

    } Collections.sort(theStrings);   //here
    for (String s : theStrings) {  //here
        //...

    this.setContentView(scrollviewManuf); }//here
}

}

以下代码在每次循环添加额外重复选项时循环。

即。猫,狗,老鼠,驴正确的清单是列表,但我得到,猫,狗.dog,鼠标,鼠标,鼠标,驴,驴,驴,驴但仍然没有排序,仍在努力,但这里是代码

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

        for (int i = 0; i < 28; i++) {
            int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
            String Manuf = getResources().getString(id);
            theStrings.add(Manuf);
            Collections.sort(theStrings);
        for (String s : theStrings) {


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

            Manufb = new Button(this);
            Manufb.setText(Manuf);
            Manufb.setId(i);
            Manufb.setTextSize(30);
            Manufb.setPadding(0, 0, 0, 0);

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

            Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            linearManuf.addView(Manufb);

            Manufb.setOnClickListener(new View.OnClickListener() {


                @Override

                public void onClick(View v) {
                    String PManuf = Manuf;
                    // TODO Auto-generated method stub

                    String SManuf = Manuf.replaceAll("&", "").replaceAll(" ", "").replaceAll("/", "").replaceAll(" / ", "").replaceAll("/ ", "").replaceAll(" /", "".replaceAll("&amp;", ""));
                    //Panel= getResources().getString(id);
                    Toast.makeText(getApplicationContext(), Manuf + " Selected", Toast.LENGTH_SHORT).show();

                    Intent passIntent = new Intent(Manufacturers.this, panels.class);
                    passIntent.putExtra("SManuf", SManuf);
                    passIntent.putExtra("PManuf", PManuf);
                    startActivity(passIntent);
                }

            });

        }
    }
    this.setContentView(scrollviewManuf);
}
}

1 个答案:

答案 0 :(得分:0)

将其读入列表,对其进行排序并循环遍历:

ArrayList<String> theStrings = new ArrayList<>();
 for (int i = 0; i < 28; i++) {
    int id = getResources().getIdentifier("Manuf" + i, "string", getPackageName());
    String Manuf = getResources().getString(id);
    theStrings.add(Manuf);
}
Collections.sort(theStrings);

for (String s : theStrings) {

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

    Manufb = new Button(this);

    Manufb.setText(s);  // <-- use the String here
    Manufb.setId(i);
    Manufb.setTextSize(30);
    Manufb.setPadding(0, 0, 0, 0);

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

    Manufb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    linearManuf.addView(Manufb);

...