【问题标题】:populating a spinner with unique strings用唯一的字符串填充微调器
【发布时间】:2013-09-04 03:19:09
【问题描述】:

我试图根据用户在对话框中输入的唯一字符串填充操作栏微调器,例如,用户输入一个字符串,如果它还没有在微调器中,它应该在那里添加自己。这样的实现可能吗?我尝试使用 ArrayList 类,但当然存在重复项,我应该使用 hashset 吗?谢谢

// array of sample strings to popluate dropdown list
ArrayList<String> categories = new ArrayList<String>();

// create an array adapter to popluate dropdown list
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getBaseContext(),
            android.R.layout.simple_spinner_dropdown_item, categories);

 //thats the alert dialog through which user enters strings
 AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("New category");
        alert.setMessage("Please enter a new category ");

        // Set an EditText view to get user input
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        Editable value = input.getText();
                        // Do something with value!

                        categories.add(value.toString());

                    }
                });

【问题讨论】:

    标签: java android android-actionbar spinner unique


    【解决方案1】:

    我建议在添加类别之前检查它是否已经使用 ArrayList.contains 添加: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)

    要考虑的另一件事是区分大小写,也许您可​​以转换为小写之前检查它是否已存在于arraylist中,如果不存在则仅添加。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多