选择具有姓名和电话号码的多个联系人 [英] Pick multiple contact with name and phone number

查看:100
本文介绍了选择具有姓名和电话号码的多个联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的代码只能选择一个联系人并显示在选定的editText中,单击添加按钮后,详细信息将插入数据库中. 现在我想选择多个联系人并将其插入数据库.对我来说,实现此目的的最佳方法是什么?

Currently my code can pick one contact only and display in the selected editText and after button add clicked, the details will inserted into the database.. Now i want to select multiple contact and insert them into the database.What is the best method for me to implement this?

下面是我当前的代码.

public class newBill extends AppCompatActivity implements View.OnClickListener{

    //Defining views
    private EditText description;
    private EditText person_engaged;
    private EditText amount;
    private EditText contact;
    private String username;
    private Button buttonAdd;
    private Button buttonView;


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

        ((Button)findViewById(R.id.btnBrowse)).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, 1);
            }
        });

        //Initializing views

        description = (EditText) findViewById(R.id.description);
        person_engaged = (EditText) findViewById(R.id.person_engaged);
        amount = (EditText) findViewById(R.id.amount);
        contact=(EditText) findViewById(R.id.person_engaged_contact);

        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        buttonView = (Button) findViewById(R.id.buttonView);

        //Setting listeners to button
        buttonAdd.setOnClickListener(this);
        buttonView.setOnClickListener(this);
    }

    //get contact picker
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Uri uri = data.getData();

            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver().query(uri, new String[]{
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                            null, null, null);

                    if (c != null && c.moveToFirst()) {
                        String number = c.getString(0);
                        String name = c.getString(1);
                        showSelectedNumber(name, number);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }

    public void showSelectedNumber(String name, String number) {
        Toast.makeText(this, name + ": " + number, Toast.LENGTH_LONG).show();
        person_engaged.setText(name);
        contact.setText(number);

    }

    //Adding an bill
    private void addBills(){

        final String desc = description.getText().toString().trim();
        final String person = person_engaged.getText().toString().trim();
        final String amo = amount.getText().toString().trim();
        final String contacts = contact.getText().toString().trim();

        //Fetching username from shared preferences
        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        username = sharedPreferences.getString(Config.USERNAME_SHARED_PREF,"Not Available");

        final String currentUser = username.toString().trim();

        class AddBills extends AsyncTask<Void,Void,String>{

            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(newBill.this,"Adding...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(newBill.this,s,Toast.LENGTH_LONG).show();
                description.getText().clear();
                person_engaged.getText().clear();
                amount.getText().clear();
                contact.getText().clear();

            }

            @Override
            protected String doInBackground(Void... v) {
                HashMap<String,String> params = new HashMap<>();
                params.put(Config.KEY_DESCRIPTION,desc);
                params.put(Config.KEY_PERSON_ENGAGED,person);
                params.put(Config.KEY_AMOUNT,amo);
                params.put(Config.KEY_PERSON_ENGAGED_CONTACT,contacts);
                params.put(Config.KEY_CREATED_BY_NAME,currentUser);

                RequestHandler rh = new RequestHandler();
                String res = rh.sendPostRequest(Config.URL_ADD, params);
                return res;
            }
        }

        AddBills ae = new AddBills();
        ae.execute();
    }

    @Override
    public void onClick(View v) {
        if(v == buttonAdd){
            addBills();
        }

        if(v == buttonView){
            startActivity(new Intent(this,ViewAllBills.class));
        }
    }

    //for sending direct sms notification
    public void sendSMS(String phoneNo, String msg) {
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, msg, null, null);
            Toast.makeText(getApplicationContext(), "Message Sent",
                    Toast.LENGTH_LONG).show();
        } catch (Exception ex) {
            Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
                    Toast.LENGTH_LONG).show();
            ex.printStackTrace();
        }
    }
}

推荐答案

您不能使用Intent.ACTION_GET_CONTENT意向选择多个联系人. 您可以自己查询所有联系人及其电话号码,在ListView中将其显示给用户,并允许用户在您自己的应用程序中选择联系人.

You can't use the Intent.ACTION_GET_CONTENT intent to pick multiple contacts. You can query for all contacts and their phone numbers yourself, display them in a ListView to the user, and allow the user to select the contacts within your own app.

要查询电话的联系人(如果您使用的是Contacts权限. >运行时权限),您可以执行以下操作:

To query for the phone's contacts (requires the Contacts permission if you're using runtime-permissions) you can do this:

List<String> allPhones = new ArrayList<>();
// The Phone class should be imported from CommonDataKinds.Phone
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, Phone.IN_VISIBLE_GROUP + "=1", null, Phone.TIMES_CONTACTED + " DESC"); 
while (cursor != null && cursor.moveToNext()) {
    String name = cursor.getString(0);
    String number = cursor.getString(1);
    allPhones.add(name + " - " + number);
}
// display allPhones in a ListView on screen, and handle item clicks

这篇关于选择具有姓名和电话号码的多个联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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