Need
ContactPickerActivity.java
ResultActivity.java
activity_read_all_contact.xml
result.xml
Step 1:
ContactPickerActivity.java
public class ContactPickerActivity extends AppCompatActivity implements View.OnClickListener { Button button; ListView listView; ArrayAdapter<String> adapter; ArrayList<String> StoreContacts ; Cursor cursor ; String name, phonenumber ; public static final int RequestPermissionCode = 1 ; /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity3_contact_picker); findViewsById(); StoreContacts = new ArrayList<String>(); EnableRuntimePermission(); GetContactsIntoArrayList(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, StoreContacts); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); listView.setAdapter(adapter); button.setOnClickListener(this); } private void findViewsById() { listView = (ListView) findViewById(R.id.list); button = (Button) findViewById(R.id.testbutton); } public void onClick(View v) { SparseBooleanArray checked = listView.getCheckedItemPositions(); ArrayList<String> selectedItems = new ArrayList<String>(); for (int i = 0; i < checked.size(); i++) { // Item position in adapterint position = checked.keyAt(i); // Add sport if it is checked i.e.) == TRUE!if (checked.valueAt(i)) selectedItems.add(adapter.getItem(position)); } String[] outputStrArr = new String[selectedItems.size()]; for (int i = 0; i < selectedItems.size(); i++) { outputStrArr[i] = selectedItems.get(i); } Intent intent = new Intent(getApplicationContext(), ResultActivity.class); // Create a bundle objectBundle b = new Bundle(); b.putStringArray("selectedItems", outputStrArr); // Add the bundle to the intent.intent.putExtras(b); // start the ResultActivitystartActivity(intent); } public void GetContactsIntoArrayList(){ cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null); while (cursor.moveToNext()) { name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); StoreContacts.add(name + " " + ":" + " " + phonenumber); } cursor.close(); } public void EnableRuntimePermission(){ if (ActivityCompat.shouldShowRequestPermissionRationale( ContactPickerActivity.this, Manifest.permission.READ_CONTACTS)) { Toast.makeText(ContactPickerActivity.this,"CONTACTS permission allows us to Access CONTACTS app", Toast.LENGTH_LONG).show(); } else { ActivityCompat.requestPermissions(ContactPickerActivity.this,new String[]{ Manifest.permission.READ_CONTACTS}, RequestPermissionCode); } } @Override public void onRequestPermissionsResult(int RC, String per[], int[] PResult) { switch (RC) { case RequestPermissionCode: if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(ContactPickerActivity.this,"Permission Granted, Now your application can access CONTACTS.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(ContactPickerActivity.this,"Permission Canceled, Now your application cannot access CONTACTS.", Toast.LENGTH_LONG).show(); } break; } } }Step 2:public class ResultActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); Bundle b = getIntent().getExtras(); String[] resultArr = b.getStringArray("selectedItems"); ListView lv = (ListView) findViewById(R.id.outputList); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultArr); lv.setAdapter(adapter); } } Step 3:result.xml<?xml version="1.0" encoding="utf-8"?><ListView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/outputList" android:layout_width="fill_parent"android:layout_height="fill_parent" />Step 4:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" > <Buttonandroid:id="@+id/testbutton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="@string/submit" /> <ListView android:id="@+id/list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@id/testbutton"android:layout_alignParentTop="true"/> </RelativeLayout> Step 5:<uses-permission android:name="android.permission.READ_CONTACTS" />
No comments:
Post a Comment