如何为SearchDialog实现TextWatcher

时间:2012-03-22 09:15:46

标签: android

我需要根据ListView中的用户输入更新我的SearchDialog。为此,我需要为TextWatcher设置SearchDialog。这该怎么做?

2 个答案:

答案 0 :(得分:0)

你可以这样做:

yourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // process new items for list view

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // push new items into adapter and call notifyDataSetChanged() on it

    }
});

答案 1 :(得分:0)

这是我几个月前实现的,以达到您需要的相同目的。当我使用sqlite语句从sqlite数据库填充列表视图时,我必须根据用户输入对列表视图进行排序。不确定它是否是最好的方式,但在我的情况下它完美无缺。

因此,在我的EditText字段中,我添加了此名称(名称为searchBar):

searchString = "";
    searchBar.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        public void afterTextChanged(Editable s) {
            // this is where actually I was changing the sqlite statement which sort my list view
            names.clear();
            categories.clear();
            paths.clear();
            count.clear();
            cardId.clear();
            myCardID.clear();


            searchString = s.toString();
            Log.e("","searchString : "+searchString);
            sqlQuery = getSqlStatement(sort, collId, ascDesc,searchString);
            Log.e(""," sqlquery : "+sqlQuery);
            sqlQuery = getSqlStatement(sort, collId, ascDesc, searchString);
            Cursor cursor = userDbHelper.executeSQLQuery(sqlQuery);
            if (cursor.getCount() == 0) {
                noCards.setVisibility(View.VISIBLE);
            } else if (cursor.getCount() > 0) {
                noCards.setVisibility(View.GONE);
                    for (cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
                        objectId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
                        cardId.add(objectId);

                        title = cursor.getString(cursor.getColumnIndex("title"));
                        catTitle = cursor.getString(cursor.getColumnIndex("catTitle"));
                        if(extra!=0 && sort!=3){
                            tags.setText(catTitle);
                        }
                        int repeats = Integer.parseInt(cursor.getString(cursor.getColumnIndex("repeatsCount")));
                        count.add(repeats);

                                    String cardsm = "SELECT objectId FROM cardmedias " +
                                                    "WHERE cardId="+ 
                                                    objectId + 
                                                    " AND mediaType=" + 
                                                    5012;

                                    Cursor cardsM = userDbHelper.executeSQLQuery(cardsm);
                                    if (cardsM.getCount() == 0) {
                                        String defCard = "SELECT objectId FROM collectionmedias " +
                                                         "WHERE collectionId="+ 
                                                         collId + 
                                                         " AND mediaType=" + 
                                                         3003;

                                        Cursor getDefCard = userDbHelper.executeSQLQuery(defCard);

                                        getDefCard.moveToFirst();

                                        objectID = Integer.parseInt(getDefCard.getString(getDefCard
                                                .getColumnIndex("objectId")));

                                        String filename = "mediacollection-"+objectID;
                                        if(storageID==1){
                                            path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, getApplicationContext());
                                        } else if(storageID==2){
                                            path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
                                        }
                                        hm.put(objectID, path);

                                        path = hm.get(objectID);
                                        paths.add(path);
                                        names.add(title);
                                        categories.add(catTitle);
                                    } else if (cardsM.getCount() > 0) {
                                        for (cardsM.move(0); cardsM.moveToNext(); cardsM.isAfterLast()) {
                                            objectID = Integer.parseInt(cardsM.getString(cardsM
                                                    .getColumnIndex("objectId")));

                                            String filename = "mediacard-"+objectID;
                                            if(storageID==1){
                                                path = RPCCommunicator.getImagePathFromInternalStorage(servername, userId, filename, getApplicationContext());
                                            } else if(storageID==2){
                                                path = RPCCommunicator.getImagePathFromExternalStorage(servername, userId, filename);
                                            }
                                            hm.put(objectID, path);

                                            path = hm.get(objectID);
                                            paths.add(path);
                                            names.add(title);
                                            categories.add(catTitle);
                                        }
                                    }
                                    cardsM.close();
                    }
            }
            cursor.close();
                    // don't forget to add this!!!
            adapter.notifyDataSetChanged();


        }
    });

在我的onCreate()实际上,我在TextWatcher做同样的事情来排序我的列表视图,所以我认为你可以实现和我一样的逻辑。

如果您有任何问题,请粘贴一些代码,以便我们为您提供帮助! :)

希望这有帮助!