在android listview中显示联系人

时间:2016-06-16 06:34:44

标签: android android-arrayadapter contactscontract

这里我使用以下代码在我的应用程序中显示联系人。它只显示一个联系人。显示在最后位置的联系人。如何显示所有联系人..可以有人告诉我如何显示所有联系人。我在getNumber()方法中使用了print语句。联系人在日志中显示为打印语句..但不在设备中。

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);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

你的代码看似正确,你应该在do-while之间调试代码,并观察日志