得到联系人的详细信息,从列表视图中选择 [英] get details of contact selected from list view

查看:112
本文介绍了得到联系人的详细信息,从列表视图中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的应用程序中,我想允许用户选择将消息发送给多个联系人。使用下面的code我已经成功地获取了用户的列表中,列表视图用复选框。现在我想,当用户点击完成按钮,在电话号码的所有选定的联系人应检索EDITTEXT 格式例如约翰1 + 919898xxxxxx>,里克&LT + 919988xxxxxx> ,并包含所有电话号码的只是10位数字,即9898xxxxxx应存放在用逗号(9898xxxxxx,9988xxxxxx)分隔字符串自动。我如何能做到的要求。

 公共类ContactsActivity扩展ListActivity {

受保护的静态最终字符串变量= NULL;
公众的String []与我们联系= {};
公众诠释[]到= {};
公众的ListView myListView;


@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_contacts);
    最终按钮done_Button =(按钮)findViewById(R.id.done_Button);
    最终按钮clear_Button =(按钮)findViewById(R.id.clear_Button);
    光标mCursor = getContacts();
    startManagingCursor(mCursor);
    ListAdapter适配器=新SimpleCursorAdapter(这一点,android.R.layout.simple_list_item_multiple_choice,mCursor,
            联系方式=新的String [] {} ContactsContract.Contacts.DISPLAY_NAME,
            为=新INT [] {android.R.id.text1});
    setListAdapter(适配器);
    myListView = getListView();
    myListView.setItemsCanFocus(假);
    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    clear_Button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            Toast.makeText(getApplicationContext(),选择清除,Toast.LENGTH_SHORT).show();
            ClearSelections();
        }
    });

    / **当完成按钮按下:** /
    done_Button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            Log.i(TAG:完成按钮选择:);
            SparseBooleanArray selectedPositions = myListView.getCheckedItemPositions();
            Log.i(TAG,经过位数:+ selectedPositions.size());
            的for(int i = 0; I< selectedPositions.size();我++){
                如果(selectedPositions.get(selectedPositions.keyAt(I))==真){
                    //做的东西

                }
            }
        }
    });
}

私人无效ClearSelections(){
    诠释计数= this.myListView.getAdapter()getCount将()。
    的for(int i = 0; I<计数;我++){
        this.myListView.setItemChecked(我,FALSE);
    }
}

私人光标getContacts(){
    //运行查询
    开放的我们的uri = ContactsContract.Contacts.CONTENT_URI;
    的String []投影=新的String [] {ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.DISPLAY_NAME};
    串选择= ContactsContract.Contacts.HAS_PHONE_NUMBER +=
            +(1)+';
    的String [] selectionArgs两个= NULL;
    字符串排序顺序= ContactsContract.Contacts.DISPLAY_NAME
            +分页中局部ASC;

    返回managedQuery(URI,投影,选择,selectionArgs两个,
            排序);
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.activity_contacts,菜单);
    返回true;
}
}
 

解决方案

终于将其配置

  done_Button.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            字符串名称= NULL;
            串号= NULL;
            长[] IDS = myListView.getCheckedItemIds();
            为(长ID:IDS){
                光标接触= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,新的String [] {ID +},NULL);
                而(contact.moveToNext()){
                    名称= contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    //名称+ =名称;
                    数= contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    //号+ =号;
                }
                Toast.makeText(getApplicationContext(),名称:+名称+\ñ+号:+编号,Toast.LENGTH_LONG).show();
            }
        }
    });
 

i am designing application in which i want to allow user to select multiple contact to send messages to. I have successfully retrieved the list of user in the listview with checkbox using the following code. now i want that when the user clicks on the "DONE" button, the PHONE NUMBER of the all selected contact should be retrieved in EDITTEXT in format like John <+919898xxxxxx>, Rick <+919988xxxxxx> and also that all the phone numbers containing just 10 digits i.e "9898xxxxxx" should be stored in a string seperated by comma (9898xxxxxx, 9988xxxxxx) automatically. how can i accomplish the requirement.

public class ContactsActivity extends ListActivity {

protected static final String TAG = null;
public String[] Contacts = {};
public int[] to = {};
public ListView myListView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    final Button done_Button = (Button) findViewById(R.id.done_Button);
    final Button clear_Button =(Button) findViewById(R.id.clear_Button);
    Cursor mCursor = getContacts();
    startManagingCursor(mCursor);
    ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor,
            Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME },
            to = new int[] { android.R.id.text1 });
    setListAdapter(adapter);
    myListView = getListView();
    myListView.setItemsCanFocus(false);
    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    clear_Button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Selections Cleared", Toast.LENGTH_SHORT).show();
            ClearSelections();
        }
    });

    /** When 'Done' Button Pushed: **/
    done_Button.setOnClickListener(new View.OnClickListener() {
        public void onClick (View v){
            Log.i(TAG,":Done Button Selected:");
            SparseBooleanArray selectedPositions = myListView.getCheckedItemPositions();
            Log.i(TAG,"Number of Checked Positions: " + selectedPositions.size());
            for (int i=0; i<selectedPositions.size(); i++) {
                if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
                    //do stuff

                }
            }
        }
    });
}

private void ClearSelections() {
    int count = this.myListView.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myListView.setItemChecked(i, false);
    }
}

private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
                                    ContactsContract.Contacts.DISPLAY_NAME};
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '"
            + ("1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs,
            sortOrder);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_contacts, menu);
    return true;
}
}

解决方案

configured it finally

        done_Button.setOnClickListener(new View.OnClickListener() {
        public void onClick (View v){
            String name = null;
            String number = null;
            long [] ids = myListView.getCheckedItemIds();
            for(long id : ids) {
                Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
                while(contact.moveToNext()){
                    name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    //name+=name;
                    number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    //number+=number;
                }
                Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
            }
        }
    });

这篇关于得到联系人的详细信息,从列表视图中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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