我有一项活动,我在列表视图中显示来自手机的联系人。现在我添加了通过调用联系意图添加新联系人来添加新联系人的功能。这很好地完成了。
但是当我在添加联系人后返回活动时,我看不到最新添加的联系人。
我试图通过调用invalidate()也尝试通过调用invalidate()onResume方法尝试的适配器来刷新列表视图。
但我无法得到结果。我还尝试使用startActivityForResult调用add intent并获取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);
}
}
答案 0 :(得分:0)
试试这个:
inside yourr onResume() add this
if (listvew!= null) {
listvew.invalidateViews();
}
adapter.notifyDataSetChanged();
答案 1 :(得分:0)
在onResume()
if(adapter!= null){
listvew.setAdapter(adapter);
adapter.notifyDataSetChanged();
}