2016-06-16 1 views
1

Ici j'utilise le code ci-dessous pour afficher les contacts dans mon application.Il affiche un seul contact.le contact qui est là dans la dernière position est affiché..Comment afficher tous les contacts..can Quelqu'un m'a dit comment afficher tous les contacts. J'ai utilisé l'instruction d'impression dans la méthode getNumber(). Les contacts sont affichés dans le journal en tant que déclaration d'impression, mais pas dans l'appareil.Affichage des contacts dans android listview

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 <String> aa= new ArrayList<String>(); 

    @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<String> adapter = new ArrayAdapter<String>(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<?> 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); 
     } 
    } 

} 

Répondre

1

votre code semble bon, vous devez déboguer le code entre do-temps, et en regardant le journal

+0

dans le journal il montre comme this..'06-16 06: 48: 48,495 19357-19357 /com.example.miisky I/System.out: .................. (934) 134-0660 06-16 06: 48: 48.495 19357-19357/com .example.miisky I/System.out: .................. sachin 06-16 06: 48: 48.503 19357-19357/com.example.miisky I/Système .out: .................. (996) 656-0754 06-16 06: 48: 48.503 19357-19357/com.example.miisky I/System.out : .................. prajwal' –