使用OnItemCLick

时间:2019-02-22 19:56:15

标签: java android arraylist android-arrayadapter custom-arrayadapter

这是一个奇怪的问题,几乎就像一个错误。 所以我正在使用一个arraylist和一个扩展ArrayAdapter的customlistview。

arraylist填充customlistview,我在操作栏中实现了一个搜索框 过滤自定义适配器,顺便可以正常工作。

问题是我试图使用过滤后的数据,所以我在列表视图上使用OnItemCLickListener来使用索引并获取与过滤后的结果相对应的数据。

但是这里发生的是数据被过滤,arraylist本身也被过滤。

说当我打开活动时,它显示我所有联系人 所以我用搜索栏搜索一个给我的名字

说保罗

现在,当我单击paul时,搜索栏过滤后paul处于0索引处,我希望得到paul的数据,但是我得到了arraylist原始0索引数据的数据

说是生病

所以在OniItelClickListiner内部,我得到的是保罗的数据,其中是保罗的

这是代码

public class sendSms extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    ListView listView;
    ArrayList<contact> arrayList;
    CustomArrayAdapterForContact customArrayAdapterForContact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);

        listView = (ListView) findViewById(R.id.showContactsListView);

        arrayList = new ArrayList<contact>();
        customArrayAdapterForContact = new CustomArrayAdapterForContact(this, arrayList);
        listView.setAdapter(customArrayAdapterForContact);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), typeMessage.class);

                intent.putExtra("id", arrayList.get(position).contact_id);
                intent.putExtra("name", arrayList.get(position).display_name);
                intent.putExtra("number", arrayList.get(position).display_number);

                startActivity(intent);
            }
        });

        getSupportLoaderManager().initLoader(5, null, this);
    }


    class CustomArrayAdapterForContact extends ArrayAdapter<contact> {


        ArrayList<contact> contact;

        public CustomArrayAdapterForContact(@NonNull Context context, ArrayList<contact> contact) {
            super(context, 0, contact);
            this.contact = contact;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            contact innserContact = getItem(position);

            if (convertView == null){

                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);

            }

            TextView username = (TextView) convertView.findViewById(R.id.subject);
            TextView userNumber = (TextView) convertView.findViewById(R.id.address);

            try {
                if (innserContact.display_name.length() > 30) {
                    username.setText(innserContact.display_name.substring(0, 30));
                }else{
                    username.setText(innserContact.display_name);
                }

                userNumber.setText(innserContact.display_number);
            }catch (Exception e){
                e.printStackTrace();
            }

            return convertView;

        }

    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int i, @Nullable Bundle bundle) {
        String[] projection = {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER
        };

        return new CursorLoader(
                this,
                ContactsContract.Contacts.CONTENT_URI,
                projection,
                null,
                null,
                ContactsContract.Contacts.DISPLAY_NAME
        );
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {

        while(cursor.moveToNext()){

            String con_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String con_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0){
                ContentResolver cr = getContentResolver();
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{con_id}, null);

                while (pCur.moveToNext()) {

                    String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    contact con = new contact(con_id, con_name, phoneNo);
                    arrayList.add(con);

                    break;
                }
                pCur.close();
            }


        }
        customArrayAdapterForContact.notifyDataSetChanged();

    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.search_box, menu);

        MenuItem item = menu.findItem(R.id.app_bar_search);

        SearchView searchView = (SearchView)item.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                customArrayAdapterForContact.getFilter().filter(newText);

                return true;
            }
        });

        return super.onCreateOptionsMenu(menu);

    }


}

过滤后

After the filtering

单击后

After clicking

2 个答案:

答案 0 :(得分:1)

注意-未经项目测试。 代替

intent.putExtra("id", arrayList.get(position).contact_id);
intent.putExtra("name", arrayList.get(position).display_name);
intent.putExtra("number", arrayList.get(position).display_number);

尝试使用

intent.putExtra("id", parent.getItemAtPosition(position).contact_id);
intent.putExtra("name", parent.getItemAtPosition(position).display_name);
intent.putExtra("number", parent.getItemAtPosition(position).display_number);

还请记住将获取的项目转换为正确的类型。

答案 1 :(得分:0)

在onItemClick中,您正在从未过滤的列表中获取联系人,但是位置是从已过滤的列表中获取的。您应该从过滤后的集合中选择联系人。