【问题标题】:Selecting a phonebook's contact from populated listview从填充的列表视图中选择电话簿的联系人
【发布时间】:2023-04-02 15:52:01
【问题描述】:

您好,我有以下代码,它基本上是填充手机联系人列表并将其显示在列表视图中。

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

        // GET PHONE BOOK'S CONTENT
        cursor = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                null,
                null,
                null);
        startManagingCursor(cursor);

        String [] entries = {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone._ID
        };

        int [] temp = {
                android.R.id.text1,
                android.R.id.text2
        };

        SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(mContext,
                android.R.layout.simple_list_item_2,
                cursor, entries, temp);
        listView.setAdapter(listAdapter);

        if(listView != null){
            listView.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    Log.w("TAG", "SELECTING: " + listView.getAdapter().getItem(position).toString());

                    /*
                    Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                            Uri.fromParts("sms", listView.getAdapter().getItem(position).toString(), null));
                    i.putExtra("sms_body", "Some message goes here.");
                    i.setType("vnd.android-dir/mms-sms");
                    startActivity(i);
                    */
                }
            });
        }

我想做的是输出联系人的姓名以及相关的电话号码(主要号码)。这样我就可以创建一个新的意图,其中根据电话号码和我的联系人姓名预先定义了一条短信。

谁能告诉我如何做到这一点?谢谢

【问题讨论】:

    标签: android sms contacts android-contacts


    【解决方案1】:

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

        ArrayList<String> listOfName = new ArrayList<String>();
        ArrayList<String> listOfNumber = new ArrayList<String>();
        Cursor cursor = null;
        try {
            cursor = getApplicationContext().getContentResolver().query(
                    Phone.CONTENT_URI, null, null, null,
                    Phone.DISPLAY_NAME + " ASC");
            int contactIdIdx = cursor.getColumnIndex(Phone._ID);
            int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
            int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
            cursor.getColumnIndex(Phone.PHOTO_ID);
            cursor.moveToFirst();
            do {
                cursor.getString(contactIdIdx);
                String name = cursor.getString(nameIdx);
                String phoneNumber = cursor.getString(phoneNumberIdx);
                listOfName.add(name);
                listOfNumber.add(phoneNumber);
            } while (cursor.moveToNext());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listOfName);
         listView.setAdapter(adapter);
    
        if (listView != null) {
            listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String selectedFromList =(String) (listView.getItemAtPosition(position));
                    Toast.makeText(getApplicationContext(), selectedFromList, Toast.LENGTH_SHORT).show();
                }
            });
        }
    

    【讨论】:

    • 谢谢,但我想你误解了我的意思。我正在尝试输出(即在 Logcat 上显示)在列表视图上单击的项目的名称和电话号码。
    【解决方案2】:

    你所要做的就是把这一行放在你的 (list on item click lisetener):

    Log.w("TAG", "SELECTING: " + parent.getItemAtPosition(position).toString());
    

    代替

    Log.w("TAG", "SELECTING: " + listView.getAdapter().getItem(position).toString());
    

                                   **Edited**
    

    要实现您的要求,可以通过以下方式完成:

    创建一个 SetterGetter 类并将这些东西放入其中,而不是放入 [String array]。

    String [] entries = {
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    ContactsContract.CommonDataKinds.Phone._ID}
    

    然后制作该 Setter-Getter 类的 ArrayList,并在填充数据后将所有 Setter-Getter 类添加到此 ArrayList 中。

    将此 ArrayList 传递给适配器类。

    在您的 OnItemClickListener 中,您的电话和姓名可以是这样的:

    Your_ArrayList.get(position).getName();
    Your_ArrayList.get(position).getPhoneNumber();
    

    你就完成了。 这很难理解,但是如果您尝试进行我提到的 StepByStep 实现,您将实现目标。

    【讨论】:

    • 您好,您建议的解决方案与我的类似。我想要做的不是将联系人作为对象获取,而是获取该对象的名称(字符串)和电话号码(字符串)。
    猜你喜欢
    • 1970-01-01
    • 2015-05-31
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多