筛选列表活动
我需要为 ListActivity 创建一个过滤器,每个项目都是一个对象。 我的想法是创建一个 TextView 并在每次按键时过滤列表。
对不起我的英语,我来自阿根廷,我的英语很糟糕。
非常感谢
private ContentResolver cr;
private TextView lblMensaje;
private int ordenBan = 1; // 1 = asc
private ArrayList<Contacto> contactos = new ArrayList<Contacto>();
private ArrayAdaptador adapter;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
listarContactos("ASC");
// registerForContextMenu(lblMensaje);
registerForContextMenu(getListView()); // Le asocio a la lista un menu
// contextual
}
public void listarContactos(String orden) {
cr = getContentResolver();
Cursor cur;
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " " + orden);
recorrerCursor("1", cur); // Listo los favoritos
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " " + orden);
recorrerCursor("0", cur); // Listo los contactos normales
this.setListAdapter(new ArrayAdapter<Contacto>(this,
R.layout.filalayout, R.id.nombre, contactos));
adapter = new ArrayAdaptador(this, R.id.nombre,
contactos);
setListAdapter(adapter);
}
public void recorrerCursor(String fav, Cursor cur) {
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String esFavorito = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.STARRED));
if (esFavorito.equals(fav)) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Contacto contacto = new Contacto();
contacto.setDisplayName(name);
if (fav.equals("1")) {
contacto.setFavorito(true);
} else {
contacto.setFavorito(false);
}
contacto.setId(id);
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
contacto.setPhone(this.getPhoneNumbers(id));
}
contacto.setEmail(this.getEmailAddresses(id));
contacto.setPhoto(this.getPhotoUri(id));
contactos.add(contacto);
}
}
}
}
I need to create a filter for a ListActivity, each items is an object.
My idea is to create a TextView and filter the list every time you press a key.
Sorry for my English, I'm from Argentina and my English is bad.
thank you very much
private ContentResolver cr;
private TextView lblMensaje;
private int ordenBan = 1; // 1 = asc
private ArrayList<Contacto> contactos = new ArrayList<Contacto>();
private ArrayAdaptador adapter;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
listarContactos("ASC");
// registerForContextMenu(lblMensaje);
registerForContextMenu(getListView()); // Le asocio a la lista un menu
// contextual
}
public void listarContactos(String orden) {
cr = getContentResolver();
Cursor cur;
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " " + orden);
recorrerCursor("1", cur); // Listo los favoritos
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " " + orden);
recorrerCursor("0", cur); // Listo los contactos normales
this.setListAdapter(new ArrayAdapter<Contacto>(this,
R.layout.filalayout, R.id.nombre, contactos));
adapter = new ArrayAdaptador(this, R.id.nombre,
contactos);
setListAdapter(adapter);
}
public void recorrerCursor(String fav, Cursor cur) {
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String esFavorito = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.STARRED));
if (esFavorito.equals(fav)) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Contacto contacto = new Contacto();
contacto.setDisplayName(name);
if (fav.equals("1")) {
contacto.setFavorito(true);
} else {
contacto.setFavorito(false);
}
contacto.setId(id);
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
contacto.setPhone(this.getPhoneNumbers(id));
}
contacto.setEmail(this.getEmailAddresses(id));
contacto.setPhoto(this.getPhotoUri(id));
contactos.add(contacto);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上并不是那个(@Kurtis)的重复,因为该链接不显示对象数组。我发现这个链接非常有帮助:
http://www .mokasocial.com/2010/07/arrayadapte-filtering-and-you/
它展示了如何实现将针对对象数组进行过滤的自定义过滤器。
It's not really a duplicate for that one (@Kurtis) because that link does not show an array of objects. I've found this link to be very helpful:
http://www.mokasocial.com/2010/07/arrayadapte-filtering-and-you/
It shows how to implement a custom filter that will filter against an array of objects.