我想在联系人应用中应用Android手机等搜索过滤器。例如,有一个名为 Aakash sharma 的联系人。在搜索栏中,我按下 Aakash 的后面,即 aa ,然后在空格后按下姓氏,即 sh 。请查找附件,以便了解击球手。请帮助我。非常感谢你提前。
这是我的代码
EditText search_filter = (EditText) stn_dialog
.findViewById(R.id.et_search_ds);
ListView listView = (ListView) stn_dialog
.findViewById(R.id.lv_search_ds);
stn_adapter = new ListAdapterStation(
RetailerSelectionActivity.this, R.layout.station_list,
station_list);
filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
// Filter from my adapter
stn_adapter.getFilter().filter(s);
// Update my view
stn_adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
};
search_filter.addTextChangedListener(filterTextWatcher);
listView.setAdapter(stn_adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int pos, long id) {
stn_dialog.dismiss();
RetailerSelectionDTO retailerSelectionDTO = (RetailerSelectionDTO) station_list
.get(pos);
station_name = retailerSelectionDTO.getStation_name_c();
station_id = retailerSelectionDTO.getStation_id_c();
btn_station.setText(station_name);
btn_retailer.requestFocus();
new GetRetailerTask().execute();
}
});
适配器
private class ListAdapterStation extends ArrayAdapter {
/**
* List of Procedure
*
*/
private ArrayList<RetailerSelectionDTO> items;
/**
* Application Context
*/
private Context mContext;
/**
* List of Procedure
*/
private ArrayList<RetailerSelectionDTO> originalItems = new ArrayList<RetailerSelectionDTO>();
private ListAdapterItemFilter filter;
public ListAdapterStation(Context context, int textViewResourceId,
List<RetailerSelectionDTO> list) {
super(context, textViewResourceId, list);
this.items = (ArrayList<RetailerSelectionDTO>) list;
this.mContext = context;
cloneItems((ArrayList<RetailerSelectionDTO>) list);
}
/**
* cloneItems @
*
* @param list
*/
protected void cloneItems(ArrayList<RetailerSelectionDTO> list) {
for (Iterator<RetailerSelectionDTO> iterator = list.iterator(); iterator
.hasNext();) {
RetailerSelectionDTO p = iterator.next();
originalItems.add(p);
}
}
/**
* Get Count
*/
@Override
public int getCount() {
return items != null ? items.size() : 0;
}
/**
* Get Item
*
* @param item
* @return TranUserProcedureDTO
*/
@Override
public RetailerSelectionDTO getItem(int item) {
RetailerSelectionDTO p = null;
p = (items != null ? items.get(item) : null);
return p;
}
/**
* Get View
*
* @param position
* @param convertView
* @param parent
* @return View
*/
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
try {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.station_list, null); // --CloneChangeRequired(list_item)
} else {
view = convertView;
}
RetailerSelectionDTO retailerSelectionDTO = items.get(position);
if (retailerSelectionDTO != null) {
TextView textView = (TextView) view
.findViewById(R.id.tv_station);
textView.setText(retailerSelectionDTO.getStation_name_c());
}
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
public Filter getFilter() {
if (filter == null) {
filter = new ListAdapterItemFilter();
}
return filter;
}
private class ListAdapterItemFilter extends Filter {
protected FilterResults performFiltering(CharSequence prefix) {
/**
* Initiate our results object
*/
FilterResults results = new FilterResults();
/**
* No prefix is sent to filter by so we're going to send back
* the original array
*/
if (prefix == null || prefix.length() == 0) {
results.values = originalItems;
results.count = originalItems.size();
} else {
/**
* Compare lower case strings
*/
String prefixString = prefix.toString().toLowerCase();
final ArrayList<RetailerSelectionDTO> filteredItems = new ArrayList<RetailerSelectionDTO>();
final ArrayList<RetailerSelectionDTO> localItems = new ArrayList<RetailerSelectionDTO>();
localItems.addAll(originalItems);
final int count = localItems.size();
for (int i = 0; i < count; i++) {
final RetailerSelectionDTO item = localItems.get(i);
final String itemName = item.getStation_name_c()
.toLowerCase();
if (itemName.contains(prefixString)) {
filteredItems.add(item);
} else {
}
}
results.values = filteredItems;
results.count = filteredItems.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
/**
*
*/
protected void publishResults(CharSequence prefix,
FilterResults results) {
/**
* noinspection unchecked
*/
final ArrayList<RetailerSelectionDTO> localItems = (ArrayList<RetailerSelectionDTO>) results.values;
notifyDataSetChanged();
clear();
/**
* Add the items back in
*/
for (Iterator iterator = localItems.iterator(); iterator
.hasNext();) {
RetailerSelectionDTO gi = (RetailerSelectionDTO) iterator
.next();
add(gi);
}
}
}
}
答案 0 :(得分:0)
使用自动填充textview。 请参阅此链接http://www.javatpoint.com/android-autocompletetextview-example
答案 1 :(得分:0)
您需要将前缀和项目名称拆分为单词,然后匹配。
for (int i = 0; i < count; i++) {
final RetailerSelectionDTO item = localItems.get(i);
final String itemName = item.getStation_name_c()
.toLowerCase();
final String[] componentsItemName = itemName.split(" ");
final String[] componentsPrefixString = prefixString.split(" ");
// now here, you should match each word of prefix with each word of itemname
}