没能获得搜索结果的确切位置在列表视图中的Andr​​oid [英] Not able to get search results exact position in listview in android

查看:210
本文介绍了没能获得搜索结果的确切位置在列表视图中的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的接触ID都坐在称为ContactsID一个ArrayList和它的名字被存储在一个名为ContactsName另一个数组列表。我在一个列表视图中显示的名称与搜索功能

All my contacts ID are sitting in an arraylist called ContactsID and it's name is stored in another arraylist called ContactsName. I am displaying the name on a list view with search functionality

搜索功能也与精细过滤器的帮助下工作。

search function is also working fine with the help of filter.

我想从接触使用的位置单击的项目的电子邮件地址。

I am trying to get the email address from contact using the position of item clicked.

事情是这样的:

  String Cid = ContactsID.get(position);

我使用id获取电子邮件查询工作正常过(当我没有任何搜索)。

My query for fetching email using id works fine too (when i don't search anything)..

我唯一的问题是,如果我搜索了名列表视图被过滤用的名字。结果示于列表视图,但是根据它的名字的位置没有得到更新。它总是从位置0我将在获得contactsID的EMAIL有问题的开始。

My only problem is if I search for a "name" the listview gets filtered with names. results are shown on the listview but the positions are not getting updated according to it's name. It always starts from position 0 for which I will have problem in getting the contactsID for EMAIL.

所以,我想parent.getItemAtPosition(位置)(这被点击的项目的名称,但没有得到ContactsID ArrayList的位置)

So I tried parent.getItemAtPosition(position) (This gets the name of the item clicked but doesn't get the ContactsID arraylist position)

下面是我想。

 lisview.setOnItemClickListener(new OnItemClickListener() 
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) 
        {
            String Cid= contactsID.get(position);

            /*String SSid = (String) parent.getItemAtPosition(position);

            Log.e("ABC",""+SSid);*/

            ContentResolver cr = getContentResolver();
            Cursor cur1 = cr.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{Cid}, null); 


            while (cur1.moveToNext()) 
            { 
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Log.e("email",""+email);

            }

            cur1.close();
        }

    });

有人可以帮助我解决这个好吗?

Can somebody help me out with fixing this please?

谢谢!

推荐答案

这个问题的原因是,你有两个列表,但 ArrayAdapter 过滤机制仅适用于茎连接到 ListView控件列表。所以,当你开始按位置过滤列表从一个列表映射到另一个坏了,因为只有贴在视图列表中被修改。

The problem stems from the fact that you have two lists, but the ArrayAdapter filtering mechanism only works on the list attached to the ListView. So when you start filtering the list the mapping from one list to the other by position breaks down because only the list attached to the view is being modified.

最简单的解决方案是创建两个数据元素绑定在一起,让你的适配器管理的,而不是一个基本的POJO模型对象。例如:

The simplest solution is to create a basic POJO model object to bind the two data elements together and let your adapter manage that instead. For example:

ContactModel.java

public class ContactModel {
    public String id;
    public String name;

    public ContactModel(String id, String name) {
        this.id = id;
        this.name = name;
    }

    // Doing this allows the adapter to display the name without
    // creating a custom getView() implementation
    @Override
    public String toString() {
        return name;
    }
}

...那么你就可以在容纳你所有的数据的单一的ArrayList&LT; ContactModel&GT; ...

ArrayList<ContactModel> ContactsList = new ArrayList<ContactModel>();
//Add your items
ContactsList.add( new ContactModel(someId, someName) );

...并修改您的适配器类型为 ArrayAdapter&LT; ContactModel&GT; 。然后在点击听众...

...and modify your adapter type to be ArrayAdapter<ContactModel>. Then in your click listener...

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) 
    {
        ContactModel item = ContactsList.get(position);
        String Cid = item.id;

        /** existing code, etc. **/
    }

这样的名字显示在列表和IDS你需要查询总是联系在一起的。

This way the names displayed in the list and the ids you need for the queries are always tied together.

这篇关于没能获得搜索结果的确切位置在列表视图中的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆