【问题标题】:How do I sort the buttons generated from the following code alphabetically base on string values如何根据字符串值按字母顺序对从以下代码生成的按钮进行排序
【发布时间】:2016-05-03 12:49:09
【问题描述】:
<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 字符串文件中,想要按字母顺序排列,所以我可以根据需要向文件中添加更多内容,而编程只是按字母顺序对它进行排序,这适合我。

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

好的,所以我可以看到代码正在执行某些操作,但排序顺序没有改变,并且 String 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
}

}

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

即。 Cat,dog,mouse,donkey 正确的列表是列表,但我得到了,Cat, dog.dog, mouse,mouse,mouse, donkey, donkey, donkey,donkey 但仍然没有排序,仍在处理但这里是代码.

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

【问题讨论】:

    标签: android list sorting button alphabetical


    【解决方案1】:

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

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

    ...

    【讨论】:

    • 谢谢,我不确定我是否正确整合了这个,没有错误,但也没有排序。我会玩一会儿。
    • 添加一些代码?不遵循这一点,不确定我在做什么。我可以看到我们正在从为按钮生成的列表中将列表 theStrings 放在一起,但我看不到字符串列表是如何从字符串中获取的,除了 thestrings.add(Manuf);所以我错过了一些代码?或者我只需要弄清楚到底是什么?
    • 显示一些代码,说它通常不起作用是不够的,除非您发布您正在使用的代码,否则无法判断出什么问题
    • 我会再玩一次并发布我输入的代码/该部分的修改方式
    • 无法在此处添加太长,无法添加 1700 多个字符
    猜你喜欢
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 2016-02-18
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多