1
我一直在尋找了很多關於通過編輯文本搜索的列表視圖中的項目,儘管我發現很多問題相似,我的問題,卻處處要麼光標適配器用在篩選的結果是直接從數據庫或一些數組列表,獲取用於。如何在android中使用base adapter時過濾列表項?
在我來說,我一直在使用幾個陣列來傳遞文本使用基礎adapter.as我從來沒有做過這樣的事情我的列表視圖,我無法理解使用文本守望者。
我調試的代碼,發現該公佈結果的方法是沒有得到徵召,可能這就是爲什麼列表視圖無法獲得新的價值。 是否必須再次調用listviewadaptercontacts類,並使用文本更改方法中名稱數組內部的刷新值。
請幫忙。
public class AllContactsActivity extends ListActivity implements
android.view.View.OnClickListener, OnItemClickListener {
ListView lv;
ListViewAdapterContacts lva;
String[] names, phones, ids, types;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = getLayoutInflater();
mainLayout.addView(layoutInflater.inflate(R.layout.allcontacts, null));
mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
this.addContentView(mainLayout, params);
configureBottomMenu();
getContacts();
lv = new ListView(getApplicationContext());
lv = (ListView) findViewById(android.R.id.list);
lva = new ListViewAdapterContacts(this, names, types, phones, ids); //passing arrays to class
lv.setAdapter(lva);
et = (EditText) findViewById(R.id.searchcontact);
et.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub
lva.getFilter().filter(s);
lva.notifyDataSetInvalidated();
lva.notifyDataSetChanged();
lv.setAdapter(lva);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
//some code
}
}
public class ListViewAdapterContacts extends BaseAdapter implements Filterable {
private ArrayFilter mFilter;
private ArrayList <String> mOriginalValues;
private final Object mLock = new Object();
private List <String> mObjects;
List list;
Activity context;
String[] names;
String[] types;
String[] numbers;
String[] ids;
public ListViewAdapterContacts(Activity context, String[] names, String[] types, String[] numbers, String[] ids) {
// TODO Auto-generated constructor stub
this.context = context;
this.names = names;
this.types = types;
this.numbers = numbers;
this.ids = ids;
Object[] array = names;
list = Arrays.asList(array);
}
public int getCount() {
// TODO Auto-generated method stub
if (names == null) {
return 0;
} else {
return names.length;
}
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class viewHolder {
TextView top;
TextView bottom;
TextView downside;
TextView base;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
viewHolder holder;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.textviewonly, null);
holder = new viewHolder();
holder.top = (TextView) convertView.findViewById(R.id.toptext);
holder.bottom = (TextView) convertView.findViewById(R.id.bottomtext);
holder.downside = (TextView) convertView.findViewById(R.id.lowest);
holder.base = (TextView) convertView.findViewById(R.id.baseid);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.top.setText(names[position]);
holder.bottom.setText(types[position]);
holder.downside.setText(numbers[position]);
holder.base.setText(ids[position]);
return convertView;
}
public Filter getFilter() {
// TODO Auto-generated method stub
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized(mLock) {
//mOriginalValues = new ArrayList<String>(mObjects);
mOriginalValues = new ArrayList <String> (list);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized(mLock) {
ArrayList <String> list = new ArrayList <String> (mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
String prefixString = prefix.toString().toLowerCase();
final ArrayList <String> values = mOriginalValues;
final int count = values.size();
final ArrayList <String> newValues = new ArrayList <String> (count);
for (int i = 0; i < count; i++) {
final String value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//noinspection unchecked
mObjects = (List <String>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
爲什麼不創建一個實體類合同和使用名單而不是使用字符串[]名稱,電話,ID,類型,這是畝更容易過濾數據。 –
但問題仍然same.how做,即使我使用的實體類 –
迭代列表檢查過濾如有合約滿足任何條件,例如contract.name.equals(名稱),並把合格的一進一出新列表,然後將新列表插入適配器 –