如何根据字符串值按字母顺序对从以下代码生成的按钮进行排序


<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 中显示为未使用,:下面的新代码:-

在此处用//标记了这些部分

 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
}

}

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

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

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);
}
}

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

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

最新更新