【发布时间】:2014-10-26 21:07:56
【问题描述】:
我正在使用minSdkVersion 8 开发Android 并使用ListView 在我的应用程序中显示移动联系人,但现在我还需要为列表视图添加过滤器,但我在sdkVersion 8 中找不到任何可以使用SearchView你们能帮忙吗?这是我的代码-
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> phones = new ArrayList<String>();
ArrayList<String[]> selectedPhones = new ArrayList<String[]>();
ArrayList<String> selectedPhoneNumbers = new ArrayList<String>();
MyAdapter adapter;
Button done;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts);
ArrayList<String[]> oldContacts = (ArrayList<String[]>) getIntent()
.getSerializableExtra("contacts");
for (int i = 0; i < oldContacts.size(); i++) {
selectedPhoneNumbers.add(oldContacts.get(i)[1]);
}
getAllCallLogs(this.getContentResolver());
ListView lv = (ListView) findViewById(R.id.lvContactsSelecetion);
adapter = new MyAdapter();
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
done = (Button) findViewById(R.id.bSelectContacts);
done.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < names.size(); i++) {
if (adapter.mCheckStates.get(i) == true) {
String[] contact = new String[] {
names.get(i).toString(),
phones.get(i).toString() };
selectedPhones.add(contact);
}
}
Intent returnIntent = new Intent();
returnIntent.putExtra("contacts", selectedPhones);
setResult(RESULT_OK, returnIntent);
finish();
}
});
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
adapter.toggle(arg2);
}
public void getAllCallLogs(ContentResolver cr) {
Cursor contacts = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, "DISPLAY_NAME ASC");
while (contacts.moveToNext()) {
String name = contacts
.getString(contacts
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = contacts
.getString(contacts
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
names.add(name);
phones.add(phoneNumber);
}
contacts.close();
}
class MyAdapter extends BaseAdapter implements
CompoundButton.OnCheckedChangeListener {
private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1, tv;
CheckBox cb;
MyAdapter() {
mCheckStates = new SparseBooleanArray(names.size());
mInflater = (LayoutInflater) Contacts.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return names.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = mInflater.inflate(R.layout.row_contact, null);
tv = (TextView) vi.findViewById(R.id.tvContactName);
tv1 = (TextView) vi.findViewById(R.id.tvContactNumber);
cb = (CheckBox) vi.findViewById(R.id.cbContactSelected);
tv.setText("Name :" + names.get(position));
tv1.setText("Phone No :" + phones.get(position));
cb.setTag(position);
if (selectedPhoneNumbers.contains(phones.get(position)))
cb.setChecked(mCheckStates.get(position, true));
else
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
【问题讨论】:
标签: android listview search filter