2013-03-05 105 views
0

我加載手機通訊錄INA名單和EditText上實現TextChangedListener如下安卓的EditText與TextChangedListener問題

editTxt.addTextChangedListener(new TextWatcher() { 

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

      public void afterTextChanged(Editable s) { 
       final TextView noDataFound = (TextView) findViewById(R.id.norecords); 
       inputName = s.toString(); 
       if(inputName!=null&&!inputName.trim().equals("")){ 

       Log.d(TAG, "LoadMoreEntries --> Constants.loadEntries : " 
         + Constants.loadEntries); 

        if (Constants.loadEntries != null) { 
         Constants.loadEntries.cancel(true); 
        } 

       Constants.loadEntries = new LoadEntries(); 
       Constants.loadEntries.execute(); 
      } 
       Button closesearch = (Button) findViewById(R.id.closesearch); 

       if (inputName != null && !inputName.trim().equals("")) { 
        closesearch.setVisibility(View.VISIBLE); 
       } else { 
        closesearch.setVisibility(View.GONE); 
       } 

       closesearch.setOnTouchListener(new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         if (Constants.loadEntries != null) { 
          Constants.loadEntries.cancel(true); 
       Constants.loadEntries = new LoadEntries(); 
       Constants.loadEntries.execute(); 
         }else { 


      } 
         return false; 
        } 
       }); 

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

      } 
     }); 

這裏當用戶鍵入正確的名稱是給的名字,當他種錯名字它沒有顯示數據。 我的問題是,當我輸入正確的名稱和擦除,整個列表被加載,但當我輸入錯誤的名稱和顯示 沒有數據,當刪除名稱,列表不更新。我也有「X」按鈕後鍵入名稱,並點擊該 應該讓我所有的名單回來。任何幫助表示讚賞

回答

1

使用Google Places AutoComplete API而不是實施Textwatcher。 Google地方信息自動完成API在啓動類型並暫停時非常有效,然後它會顯示下拉列表,並且每個字符都會更新下拉列表。

使用此功能,您可以輕鬆更新自動填充的下拉列表。

這是對此的解釋。

editTxt.setAdapter(new PlacesAutoCompleteAdapter(this,R.layout.yourlayout)); 

這裏是PlacesAutoCompleteAdapter類,這是過濾後的結果,並返回篩選的結果。

private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable { 
    private ArrayList<String> resultList; 
    private String[] myArray; 

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    @Override 
    public int getCount() { 
     return myArray.length; 
    } 

    @Override 
    public String getItem(int index) { 
     return myArray[index]; 
    } 

    @Override 
    public Filter getFilter() { 
     Filter filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults filterResults = new FilterResults(); 
       if (constraint != null) { 
        // Retrieve the autocomplete results. 
        myArray = autocomplete(constraint.toString()); // here we are calling myAutocomplete method.      
        // Assign the data to the FilterResults 
        filterResults.values = myArray; 
        filterResults.count = myArray.length; 
       } 
       return filterResults; 
      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       if (results != null && results.count > 0) { 
        notifyDataSetChanged(); 
       } 
       else { 
        notifyDataSetInvalidated(); 
       } 
      }}; 
     return filter; 
    } 
} 

private String[] autocomplete(String dropdownString) { 
    ArrayList<String> resultList = null; 
    StringBuilder jsonResults = new StringBuilder(); 
    String term; 

    try { 
     term=URLEncoder.encode(dropdownString, "utf8"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     term = dropdownString; 
    } 

    StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE); 
    sb.append("?param="+param1+"); // this is parameter if your getting data from server.   
    sb.append("&term="+term); // this term which you typing in edittext. 
    String url = sb.toString(); 

     // you can do here anything with your list. get it and populate it. 

    return myArray; 
} 

PLACES_API_BASE: - here is url if you are getting data from Web(in my example www.myurl/myapp)
TYPE_AUTOCOMPLETE: - file name or exact location from where are you getting data(in my example abc.php)
如果您有任何查詢問我。不要猶豫。

+0

@Sandip ..謝謝你的有用答案,但我想知道如何實現這個以及我在做什麼錯誤 – teekib 2013-03-05 07:51:50