从联系人意图添加联系人后如何刷新列表视图?

Posted

技术标签:

【中文标题】从联系人意图添加联系人后如何刷新列表视图?【英文标题】:How to refresh list view after adding the contact from contact intent? 【发布时间】:2016-09-24 08:12:32 【问题描述】:

我有一个活动,我在列表视图中显示来自电话的联系人。现在我添加了通过调用联系人意图添加新联系人来添加新联系人的功能。这做得很好。

但是当我在添加联系人后返回活动时,我看不到最新添加的联系人。

我尝试通过在适配器上调用 invalidate() 来刷新列表视图,我尝试通过调用 invalidate() onResume 方法。

但我无法得到结果。我还尝试使用 startActivityForResult 调用添加意图并在 onActivityResult() 中获取联系人,但这也失败了。

不知道怎么回事。请帮忙。

活动:

public class ContactList extends AppCompatActivity 


    private ArrayList<contact> contact_list = null;
    private contactAdapter mContactAdapter = null;

    private ArrayList<contact> items;
    private ArrayList<contact> selectedContacts;
    boolean[] isChecked;
    Cursor mCursor;
    ListView lv;
    public int RQS_PICK_CONTACT = 1,RQS_ADD_CONTACT = 2;
    private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
    ArrayList<Integer> selectedItemsPositions;
    private ImageView done,back;
    private boolean mContactListActivity;
    private FloatingActionButton fab;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_contacts_list);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar.setTitle("");
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

        toolbarTitle.setText("Select Contacts");

        setSupportActionBar(toolbar);

        done = (ImageView)findViewById(R.id.done);

        back = (ImageView)findViewById(R.id.back);

        fab = (FloatingActionButton)findViewById(R.id.fab);

        contact_list = new ArrayList<contact>();

        selectedContacts = new ArrayList<contact>();

     //   selectedItemsPositions = new ArrayList<>();

        lv = (ListView)findViewById(R.id.list);

        showContacts();

        //getContacts();


        fab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 


                Intent intent = new Intent(Intent.ACTION_INSERT,
                        ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent,RQS_ADD_CONTACT);


            
        );


    

    @SuppressWarnings("unused")
    private void getContacts() 


        String[] projection = new String[] 
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts.HAS_PHONE_NUMBER,
                ContactsContract.Contacts._ID ;


       mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

        while (mCursor.moveToNext()) 
            contact contact = new contact();

            String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));

            contact.setContactid(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID)));
            contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            contact_list.add(contact);
        
        isChecked = new boolean[mCursor.getCount()];

        for (int i = 0; i < isChecked.length; i++) 
            isChecked[i] = false;
        


        this.mContactAdapter = new contactAdapter(this, R.layout.contact_list_item, contact_list);
        lv.setAdapter(this.mContactAdapter);
        mContactAdapter.notifyDataSetChanged();
     //   mCursor.close();

    


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RQS_PICK_CONTACT) 
            if (resultCode == RESULT_OK) 

                getContacts();

            
        

    


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) 
        if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) 
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                // Permission is granted
                getContacts();
             else 
                Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
            
        
    


    private void showContacts()
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) 

            requestPermissions(new String[]Manifest.permission.READ_CONTACTS, PERMISSIONS_REQUEST_READ_CONTACTS);
            //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
        

        else 

            getContacts();

        

    

    @Override
    public void onResume()
    

        super.onResume();

        mContactAdapter.notifyDataSetInvalidated();

        //Intent intent = getIntent();
        //intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        //finish();
        //startActivity(intent);

    


【问题讨论】:

【参考方案1】:

试试这个:

    inside yourr onResume() add this
   if (listvew!= null) 
    listvew.invalidateViews();

 adapter.notifyDataSetChanged();

【讨论】:

试试这个 listview.invalidateViews(); @user6265109 没有帮助。 :-( @muruga5000【参考方案2】:

onResume()里面添加这个

if(adapter!= null)
    listvew.setAdapter(adapter);
    adapter.notifyDataSetChanged();

【讨论】:

以上是关于从联系人意图添加联系人后如何刷新列表视图?的主要内容,如果未能解决你的问题,请参考以下文章

谁能帮我知道如何将 ags xmpp 联系人列表添加到 XML 文件并从中显示到列表视图中

如何使用复选框将Android联系人添加到列表视图中[重复]

如何从列表视图中从 onItemClick() 获得的列表项的联系人 Uri 中检索数据?

联系人列表仅使用意图 ContactsContract 显示一个电话号码

如何删除列表视图中重复的联系人?

如何通过意图添加具有名字和姓氏的联系人