【问题标题】:remove item from List<String> in android从 Android 中的 List<String> 中删除项目
【发布时间】:2019-10-12 06:11:01
【问题描述】:

我有一个列表,即 tag_complaints1,其中包含以下值:

[#nasikPolice, #mnc, #awaaz, #college, #principal, #stalking, #eve_teasing, #bad_touch, #domestic_violence, #nasik, #help, #muncipal] 

从 multiAutoCompleteTextView 中选择数据后,获取如下数据:

[#principal #b #awaaz ] 

通过使用这个

List<String> items = Arrays.asList(tag_name);

现在我想从已存在于 tag_complaints1 中的列表项中删除 #principal #awaaz 并从列表项中仅获取 #b

      tag_name = multiAutoCompleteTextView.getText().toString();
            Log.d(TAG,"selected tags are:"+tag_name);

            if (!tag_complaints.contains(tag_name)){

                final DocumentReference documentReference7 = firebaseFirestore.collection("Button").document("Tag_option");
                documentReference7.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document != null) {
                                tag_complaints1 = (List<String>) document.get("Tags");
                                ArrayList<String> tempList= new ArrayList<String>();

                                List<String> items = Arrays.asList(tag_name);
                                Log.d(TAG,"getted string List"+items);


                                //documentReference7.update("Tags", FieldValue.arrayUnion(new_tag_name));


                            }
                        }
                    }
                });

【问题讨论】:

    标签: android list arraylist


    【解决方案1】:

    使用List#removeAll:

    if (document != null) {
        tag_complaints1 = (List<String>) document.get("Tags");
        ArrayList<String> tempList= new ArrayList<String>();
        List<String> items = Arrays.asList(tag_name);
        Log.d(TAG, "getted string List" + items);
        // CHANGE HERE:
        items.removeAll(tag_complaints1);
    }
    

    【讨论】:

    • 删除 result=[#principal #stalking #a] 后它应该不起作用
    【解决方案2】:

    从multiAutoCompleteTextView获取数据后,需要使用以下方法进行拆分:

    列表列表 = (Arrays.asList(tag_name.split(" "));

    然后执行删除操作,如:

        //remove common element
                                    for (String removestring : tag_complaints1) {
                                        if (list.contains(removestring)) {
                                            //filter item from list
                                            list.remove(removestring);
                                        }
    
                                    }
                                    Log.d(TAG,"after removing list"+list);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多