在androidstudio中单击searchview后如何保持选中复选框?

oalqel3c  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(264)

我有一个问题如下。有人能帮我吗?在我的场景中,listview中有多个复选框。在那里,我们在顶部提供了一个搜索视图。如果单击searchview,则复选框将被清除。我只想在单击searchview、执行搜索和关闭searchview时保留选中的项目。据我所知我试过这个,

final String[] nameArray1 = new String[response.getJSONArray("array1").length()];
                                final String[] nameArray2 = new String[response.getJSONArray("array2").length()];
                                final String[] nameArray3 = new String[response.getJSONArray("array3").length()];
                                final String[] nameArray4 = new String[response.getJSONArray("array4").length()];
                                final String[] nameArray5 = new String[response.getJSONArray("array5").length()];
                                JSONArray nameArrayFirst  = response.getJSONArray("design_name");
                                for (int m = 0; m < nameArrayFirst.length(); m++) {
                                    nameArray1[m]=nameArrayFirst.getString(m);
                                }
                                JSONArray nameArraySecond  = response.getJSONArray("cat_name");
                                for (int n = 0; n < nameArraySecond.length(); n++) {
                                    nameArray2[n]=nameArraySecond.getString(n);
                                }
                                JSONArray nameArrayThird  = response.getJSONArray("instockqty");
                                for (int k = 0; k < nameArrayThird.length(); k++) {
                                    nameArray3[k]=nameArrayThird.getString(k);
                                }
                                JSONArray nameArrayFourth  = response.getJSONArray("design_code");
                                for (int p = 0; p < nameArrayFourth.length(); p++) {
                                    nameArray4[p]=nameArrayFourth.getString(p);
                                }
                                JSONArray nameArrayFifth  = response.getJSONArray("imgname");
                                for (int q = 0; q < nameArrayFifth.length(); q++) {
                                    nameArray5[q]=nameArrayFifth.getString(q);
                                }

                                final CustomListAdapterForChooseSanitaryDesigns whatever = new CustomListAdapterForChooseSanitaryDesigns(ChooseSanitaryDesigns.this, nameArray1, nameArray2,nameArray3,nameArray4,nameArray5);
                                listView = (ListView) findViewById(R.id.lstView);
                                listView.setAdapter(whatever);

                                pDialog.dismiss();

                                searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
                                    @Override
                                    public void onSearchViewShown() {

                                    }

                                    @Override
                                    public void onSearchViewClosed() {

                                        //If closed Search View , lstView will return default
                                        CustomListAdapterForChooseSanitaryDesigns whatever = new CustomListAdapterForChooseSanitaryDesigns(ChooseSanitaryDesigns.this, nameArray1, nameArray2,nameArray3,nameArray4,nameArray5);
                                        listView = (ListView) findViewById(R.id.lstView);
                                        listView.setAdapter(whatever);

                                    }
                                });

                                searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
                                    @Override
                                    public boolean onQueryTextSubmit(String query) {
                                        return false;
                                    }
                                    @Override
                                    public boolean onQueryTextChange(String newText) {
                                        if (newText != null && !newText.isEmpty()) {
                                            String[] firstFound = new String[nameArray1.length];
                                            String[] secondFound = new String[nameArray2.length];
                                            String[] thirdFound = new String[nameArray3.length];
                                            String[] fourthFound = new String[nameArray4.length];
                                            String[] fifthFound = new String[nameArray5.length];
                                            //Toast.makeText(StockActivity.this, nameArray5.length, Toast.LENGTH_SHORT).show();
                                            for(int i=0;i<nameArray1.length;i++){
                                                if (nameArray1[i].toLowerCase().contains(newText.toLowerCase()) || nameArray2[i].toLowerCase().contains(newText.toLowerCase())){
                                                    firstFound[i]=nameArray1[i];
                                                    secondFound[i]=nameArray2[i];
                                                    thirdFound[i]=nameArray3[i];
                                                    fourthFound[i]=nameArray4[i];
                                                    fifthFound[i]=nameArray5[i];
                                                }
                                            }

                                            List<String> list_without_null1 = new ArrayList<String>();
                                            for(String new_string : firstFound ) {
                                                if(new_string != null && new_string.length() > 0) {
                                                    list_without_null1.add(new_string);
                                                }
                                            }
                                            firstFound = list_without_null1.toArray(new String[list_without_null1.size()]);

                                            List<String> list_without_null2 = new ArrayList<String>();
                                            for(String new_string : secondFound ) {
                                                if(new_string != null && new_string.length() > 0) {
                                                    list_without_null2.add(new_string);
                                                }
                                            }
                                            secondFound = list_without_null2.toArray(new String[list_without_null2.size()]);

                                            List<String> list_without_null3 = new ArrayList<String>();
                                            for(String new_string : thirdFound ) {
                                                if(new_string != null && new_string.length() > 0) {
                                                    list_without_null3.add(new_string);
                                                }
                                            }
                                            thirdFound = list_without_null3.toArray(new String[list_without_null3.size()]);

                                            List<String> list_without_null4 = new ArrayList<String>();
                                            for(String new_string : fourthFound ) {
                                                if(new_string != null && new_string.length() > 0) {
                                                    list_without_null4.add(new_string);
                                                }
                                            }
                                            fourthFound = list_without_null4.toArray(new String[list_without_null4.size()]);

                                            List<String> list_without_null5 = new ArrayList<String>();
                                            for(String new_string : fifthFound ) {
                                                if(new_string != null && new_string.length() > 0) {
                                                    list_without_null5.add(new_string);
                                                }
                                            }
                                            fifthFound = list_without_null5.toArray(new String[list_without_null5.size()]);

                                            String[] firstNewFound = new String[firstFound.length];
                                            String[] secondNewFound = new String[secondFound.length];
                                            String[] thirdNewFound = new String[thirdFound.length];
                                            String[] fourthNewFound = new String[fourthFound.length];
                                            String[] fifthNewFound = new String[fifthFound.length];
                                            for(int j=0;j<firstFound.length;j++){
                                                if(firstFound[j] != "" && secondFound[j] != "" && thirdFound[j] != "" && fourthFound[j] != "" && fifthFound[j] != "") {

                                                    firstNewFound[j] = firstFound[j];
                                                    secondNewFound[j] = secondFound[j];
                                                    thirdNewFound[j] = thirdFound[j];
                                                    fourthNewFound[j] = fourthFound[j];
                                                    fifthNewFound[j] = fifthFound[j];
                                                }
                                            }
                                            CustomListAdapterForChooseSanitaryDesigns whatever = new CustomListAdapterForChooseSanitaryDesigns(ChooseSanitaryDesigns.this, firstNewFound, secondNewFound,thirdNewFound,fourthNewFound,fifthNewFound);
                                            listView = (ListView) findViewById(R.id.lstView);
                                            listView.setAdapter(whatever);
                                        } else {
                                            CustomListAdapterForChooseSanitaryDesigns whatever = new CustomListAdapterForChooseSanitaryDesigns(ChooseSanitaryDesigns.this, nameArray1, nameArray2,nameArray3,nameArray4,nameArray5);
                                            listView = (ListView) findViewById(R.id.lstView);
                                            listView.setAdapter(whatever);

                                        }
                                        return true;
                                    }
                                });

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题