title: Displaying contacts in android listview
body: <p>Here i am using the below code to display contacts in my application.It is displaying only one contact.the contact which is there in the last position is displayed..How to display all contacts..can anyone tell me how to display all the contacts.?I have used print statement in getNumber() method..the contacts are displayed in log as print statement..but not in device.</p>
  
  <pre><code>public class ContactsDisplay extends AppCompatActivity implements  OnItemClickListener,FragmentDrawer.FragmentDrawerListener {
  
      private static String TAG = ContactsDisplay.class.getSimpleName();
  
      private Toolbar mToolbar;
      private FragmentDrawer drawerFragment;
  
      String phoneNumber;
      String name;
      ListView lv;
      ArrayList &lt;String&gt; aa= new ArrayList&lt;String&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //getActionBar().setDisplayShowTitleEnabled(false);
          setContentView(R.layout.contacts_display);
          mToolbar = (Toolbar) findViewById(R.id.toolbar);
          ImageView img = (ImageView) findViewById(R.id.imageView10);
          setSupportActionBar(mToolbar);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
  
          drawerFragment = (FragmentDrawer)
                  getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
          drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
          drawerFragment.setDrawerListener(this);
  
          // display the second navigation drawer view on app launch
          displayView(0);
  
          lv= (ListView) findViewById(R.id.lv);
  
         // getNumber(this.getContentResolver());
          getNumber();
      }
  
      public void getNumber()
      {
          Cursor cursor = null;
          try {
              cursor = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
              int contactIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
              int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
              int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
              int photoIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID);
              cursor.moveToFirst();
              do {
                  String idContact = cursor.getString(contactIdIdx);
                  String name = cursor.getString(nameIdx);
                  String phoneNumber = cursor.getString(phoneNumberIdx);
                  aa.add(name+"\n"+phoneNumber);
  
                  System.out.println(".................."+phoneNumber);
                  System.out.println(".................." + name);
                  //...
              } while (cursor.moveToNext());
  
              cursor.close();// close cursor
              ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
                      android.R.layout.simple_list_item_1,aa);
              lv.setAdapter(adapter);
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              if (cursor != null) {
                  cursor.close();
              }
          }
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                              long id) {
  
          Intent intent;
  
          switch (position) {
  
              default:
                  intent = new Intent(ContactsDisplay.this, Banking.class);
                  startActivity(intent);
                  break;
          }
      }
  
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          if(id == R.id.action_search){
               Toast.makeText(getApplicationContext(), vault_no, Toast.LENGTH_SHORT).show();
              return true;
             // logout();
          }
  
          if(id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  
      @Override
      public void onDrawerItemSelected(View view, int position) {
          displayView(position);
      }
  
      private void displayView(int position) {
          Fragment fragment = null;
          String title = getString(R.string.app_name);
          switch (position) {
              case 0:
                  /*fragment = new HomeFragment();*/
                  title = "Work Details1";
  
                  break;
  
              case 1:
                  /*fragment = new ProfileFragment();
                  title = getString(R.string.title_profile);
                  break;*/
                  startActivity(new Intent(this, ContactsDisplay.class));
                  return;
              case 2:
  
                  startActivity(new Intent (this,Events.class));
                  return;
                  /*fragment = new AboutFragment();
                  title = getString(R.string.title_about);
                  break;*/
              case 3:
                  fragment = new MessagesFragment();
                  title = getString(R.string.title_messages);
                  break;
  
              case 4:
                  fragment = new VideoFragment();
                  title = getString(R.string.title_video);
                  break;
              case 5:
                  fragment = new GalleryFragment();
                  title = getString(R.string.title_gallery);
                  break;
              case 6:
                  fragment = new GalleryFragment();
                  title = "Gallery";
                  break;
  
              case 7:
                  fragment = new EventsFragment();
                  title = getString(R.string.title_events);
                  break;
  
              default:
                  break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.replace(R.id.container_body, fragment);
              fragmentTransaction.commit();
  
              // set the toolbar title
              getSupportActionBar().setTitle(title);
          }
      }
  
  }
  </code></pre>