2011-07-15 11 views
1

私はarrayadpterをフィルタリングしてデータを完全にフィルタリングするときに、リストビューとarrayadapterを作成しました。しかし、リストビューがリフレッシュされない:(それだけで前のデータを示してリストビューは、フィルタリングとAndroidの配列アダプタで更新されませんでした - 助けてください

次のように私のコードは次のとおりです。

  1. ListViewコントロール内で使用のTextView: リストビュー

    の各行を表示するためのシンプルなテキストビューを
  2. ListViewコントロール:ここに検索バーと、私はTextViewの上で使用しています、リストビューのためのTextViewがある活動の

  3. のonCreateメソッドここseachCountryは、上記でテキストボックスでありますxmlを使用して国を検索(フィルタリング)します。

    countryList = (ListView) findViewById(R.id.countryList); 
    searchCountry = (EditText) findViewById(R.id.search); 
    createProfile = (Button) findViewById(R.id.createProfile);  
    countries = getResources().getStringArray(R.array.countries_array);  
    //countryArray = new ArrayAdapter<String>(this,R.layout.country_list_row , countries); 
    countryArray = new CountryArrayAdapter<String>(this,R.layout.country_list_row , countries); 
    countryList.setAdapter(countryArray);  
    countryList.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { 
         countryFrom = ((TextView) view).getText().toString();    
        } 
    }); 
    
    searchCountry.addTextChangedListener(new TextWatcher() {    
        @Override 
        public void onTextChanged(CharSequence s, int start, int before, int count) {    
        }   
        @Override 
        public void beforeTextChanged(CharSequence s, int start, int count, 
          int after) {     
        } 
    
        @Override 
        public void afterTextChanged(Editable s) { 
         SelectCountryActivity.this.countryArray.getFilter().filter(s); 
         countryArray.notifyDataSetChanged(); 
        } 
    }); 
    
    createProfile.setOnClickListener(new OnClickListener() { 
    
        @Override 
        public void onClick(View v) { 
         Intent i = new Intent(SelectCountryActivity.this,CreateProfileActivity.class); 
         i.putExtra("countryFrom", countryFrom); 
         startActivity(i); 
         finish(); 
        } 
    }); 
    
  4. ArrayListのアダプタ

    パブリッククラスCountryArrayAdapterはArrayAdapter { パブリック文字列[] allItemsを拡張します。 public String [] subItems; プライベートCountryFilterフィルタ。

    public CountryArrayAdapter(Context context, int textViewResourceId, String[] countryList) { 
        super(context, textViewResourceId, countryList); 
        // TODO Auto-generated constructor stub 
        this.subItems = countryList; 
        this.allItems = this.subItems; 
    } 
    
    @Override 
    public Filter getFilter() { 
        if (filter == null){ 
         filter = new CountryFilter(); 
        } 
        return filter; 
        } 
    
    @Override 
    public int getCount() { 
        return subItems.length; 
    } 
    
    @Override 
    public String getItem(int position) { 
        return subItems[position]; 
    } 
    
    private class CountryFilter extends Filter{ 
    
        @Override 
        protected FilterResults performFiltering(CharSequence constraint) { 
         FilterResults results = new FilterResults(); 
         ArrayList<String> i = new ArrayList<String>(); 
         String[] contents; 
    
          if (constraint!= null && constraint.toString().length() > 0) { 
    
           for (int index = 0; index < allItems.length; index++) { 
            String si = allItems[index]; 
            if(si.startsWith(constraint.toString())){ 
            i.add(si); 
            } 
           } 
           contents = new String[i.size()]; 
           i.toArray(contents); 
           results.values = contents; 
           results.count = contents.length;     
          } 
          else{ 
           synchronized (allItems){ 
            results.values = allItems; 
            results.count = allItems.length; 
           } 
          } 
          return results; 
        } 
    
        @Override 
        protected void publishResults(CharSequence constraint, 
          FilterResults results) { 
         // TODO Auto-generated method stub 
         subItems = (String[])results.values; 
         notifyDataSetChanged(); 
        } 
    } 
    

は、私は私の問題を解決しますインフレータが、インフレータの使用方法ということを把握することはできませんいくつかのリンクから知って来ます。

ビュー部分をリフレッシュする問題を解決するのを手伝ってください。

ありがとうございます。フィルタが行われ、明示的に後よろしく、リスト内 イシャン

答えて

2

セットアダプタオブジェクトと

+0

ちょっとBhushan、**ありがとうございました!**今はうまくいきます。 – Ishan

+1

答えの横に表示されているゼロを増やして回答を受け入れる必要があります。これは他の人が参照するのにも役立ちます。 –

関連する問題