0

我正在尝试在ListView. 我ListViewHashMapin组成ArrayList,我设法实现了逻辑,但我猜每次文本更改时,我的方法中有很多对象和内存分配。因此,我正在寻找更少的内存分配逻辑来搜索我ListViewHashMapinArrayList

@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            //textlength = searchBar.getText().length();
            //text_sort.clear();
            sortedArrayList.clear();


            for (int i = 0; i < myHistList.size(); i++) {

                HashMap<String, String> hash = new HashMap<String, String>(); 
                hash = myHistList.get(i);

                if (hash.get("myName").toLowerCase().indexOf(searchBar.getText().toString().toLowerCase()) != -1) {

                    String callerNum1 = hash.get("myNumber");
                    String myName1 = hash.get("myName");


                    HashMap<String, String> searchedHash = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    searchedHash.put("myNumber", callerNum1);
                    searchedHash.put("myName", myName1);
                    recordingFile1);

                    // adding HashList to ArrayList
                    sortedArrayList.add(searchedHash);

                }
            }

            ListView actualLv = mPullRefreshListView.getRefreshableView();
            actualLv.setAdapter(new myHistoryListAdapter(myHistory.this, sortedArrayList));

        }
4

1 回答 1

0

一开始你可以更换

HashMap<String, String> hash = new HashMap<String, String>(); 
hash = myHistList.get(i);

只需

 HashMap<String, String> hash = myHistList.get(i);

它将略微减少冗余对象的数量。

在第二步,如果您需要比较字符串是否相同但忽略字母的大小写,您可以尝试简化您的if条件

if (hash.get("myName").toLowerCase().indexOf(searchBar.getText().toString().toLowerCase()) != -1)

if(hash.get("myName").compareToIgnoreCase(searchBar.getText().toString()) == 0)

此外,如果您将String callerNum1 = hash.get("myNumber");上述if语句放在上面,那么您可以节省一些时间,因为您不需要通过两次 HashSet 来搜索相同的元素。它将如下所示:

String callerNum1 = hash.get("myNumber");

if(callerNum1.compareToIgnoreCase(searchBar.getText().toString()) == 0){
     ...
}
于 2013-11-13T11:19:59.593 回答