2013-10-17 179 views
0

在我的應用程序中,我有一個用戶已安裝應用程序的列表,並且希望爲該列表創建一個搜索功能。現在,這裏是我的編碼:如何爲搜索功能制定自定義過濾器

 // create new adapter 
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager()); 
// load list application 
mListAppInfo = (ListView)findViewById(R.id.lvApps); 
// set adapter to list view 
mListAppInfo.setAdapter(adapter); 
// search bar 
inputSearch = (EditText) findViewById(R.id.inputSearch); 

inputSearch.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 

     // When user changed the Text 
     // Drag_and_Drop_App.this.adapter.getFilter().filter(cs); 
     Drag_and_Drop_App.this.adapter.getFilter().filter(cs); 

    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub       
    } 
    }); 

,當我在這條線得到一個錯誤出現該問題:

Drag_and_Drop_App.this.adapter.getFilter().filter(cs); 

它說,「用getFilter()」不是我的底座適配器定義,這是這樣的:

package com.example.awesomefilebuilderwidget; 

IMPORTS 

public class AppInfoAdapter extends BaseAdapter { 
private Context mContext; 
private List mListAppInfo; 
private PackageManager mPackManager; 

public AppInfoAdapter(Context c, List list, PackageManager pm) { 
mContext = c; 
mListAppInfo = list; 
mPackManager = pm; 
} 

@Override 
public int getCount() { 
return mListAppInfo.size(); 
} 

@Override 
public Object getItem(int position) { 
return mListAppInfo.get(position); 
} 

@Override 
public long getItemId(int position) { 
return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// get the selected entry 
ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position); 

// reference to convertView 
View v = convertView; 

// inflate new layout if null 
if(v == null) { 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    v = inflater.inflate(R.layout.layout_appinfo, null); 
} 

// load controls from layout resources 
ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon); 
TextView tvAppName = (TextView)v.findViewById(R.id.tvName); 
TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack); 

// set data to display 
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager)); 
tvAppName.setText(entry.loadLabel(mPackManager)); 
tvPkgName.setText(entry.packageName); 

// return view 
return v; 
} 

@Override 
public Filter getFilter() { 
// TODO Auto-generated method stub 
return filter; 
} 
} 

我添加了最後一部分「公共過濾器...」從四處張望在stackoverflow上。但現在,我需要一個自定義篩選器進行搜索。我可以使用什麼? (我已經嘗試的一件事,但它不工作)

+1

如果您顯示的是項目列表,那麼擴展'ArrayAdapter'而不是'BaseAdapter'可能會有意義。 'ArrayAdapter'已經實現了'Filterable'接口,所以通過遷移你可以免費獲得一個基本的過濾器。另外,特別是如果你需要更多的控制實際的過濾邏輯,你可以實現你自己的'過濾器'。關於如何做到這一點,有不少例子 - [這是一個](http://stackoverflow.com/a/14369336/1029225)。 –

回答

0

getFilter()沒有定義的,因爲你的適配器必須實現過濾的接口,所以只需添加的方法您的適配器「實現的可篩選」:

public class AppInfoAdapter extends BaseAdapter implements Filterable { 
    // your stuff 
} 

,並在您用getFilter

@Override 
public Filter getFilter() { 
    if(filter == null) { 
     filter = new Filter() { 
      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 
       FilterResults results = new FilterResults(); 
       List<ApplicationInfo> myFilteredAppList = new ArrayList<ApplicationInfo>(); 
       constraint = constraint.toString().toLowerCase(); 

       for (ApplicationInfo appInfo : originalListAppInfo) { 
        String somefield = appInfo.getSomeField(); 
        if (somefield.toLowerCase().contains(constraint.toString())) { 
         myFilteredAppList.add(appInfo); 
        } 
       } 
       results.count = myFilteredAppList.size(); 
       results.values = myFilteredAppList; 
       return results; 
      } 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 
       mListAppInfo = (List<ApplicationInfo>)results.values; 
       notifyDataSetChanged(); 
      } 
     }; 
    } 
    return filter; 
} 

通知,我已經反覆throuh原始列表(originalListAppInfo)的副本,創建適配器期間創建的。

private List<ApplicationInfo> originalListAppInfo; 
private List<ApplicationInfo> mListAppInfo; 

private Filter filter; 

public AppInfoAdapter(Context context, List<ApplicationInfo> listApp) { 
    this.context = context; 
    this.originalListAppInfo = this.mListAppInfo = listApp; 
} 

希望得到這個幫助。 :)

+0

除了這一行上的兩個錯誤之外,其工作方式如下: String somefield = appInfo.getSomeField(); 和這一行: myFilteredAppList.add(site); 說「getSomeField();」是不確定的,而且「網站」不能被解析爲一個變量 – user1628978

+0

唯一的快速修復「getSomeField」是鑄造類型添加到了AppInfo – user1628978

+0

,然後「地盤」這當然是說創造的東西,但它也說,我可以將其更改爲appInfo。我應該這樣做嗎? – user1628978

相關問題