2012-03-22 66 views

回答

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 

    } 
}); 
+0

我知道這件事。我問的是我怎麼能實現這個爲SearchDialog(quicksearchbox) – 2012-03-22 09:38:30

+0

沒有顯示您的代碼,這將是很難猜測答案 – waqaslam 2012-03-22 09:43:10

+0

我想要的東西類似於QSB的SearchView.OnQueryTextListener。我無法使用SearchView.OnQueryTextListener,因爲它只支持API 11+。希望現在清楚。 – 2012-03-22 13:21:52

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排序我的列表視圖,所以我覺得你可以像我一樣實施相同的邏輯。

如果您有任何問題需要做,請粘貼一些代碼,以便我們幫助您! :)

希望這有助於!

相關問題