筛选列表活动

发布于 2024-12-10 16:37:32 字数 2700 浏览 1 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-12-17 16:37:32

它实际上并不是那个(@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文