the contactsList
is empty until the readContacts()
method was executed, in other words, when contactsView.setAdapter(adapter)
was executed, the contactsList
is empty, so why this code still can show contacts' info correctly?
public class MainActivity extends AppCompatActivity {
ListView contactsView;
ArrayAdapter<String> adapter;
List<String> contactsList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView) findViewById(R.id.contacts_list);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList);
contactsView.setAdapter(adapter);
readContacts();
}
private void readContacts() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
));
String number = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
));
contactsList.add(displayName + "\n" + number);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
but if i add something like this, i have to call notifyDataSetChanged()
:
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contactsList.add("blabla");
adapter.notifyDataSetChanged();
}
});
add
is button.
now that the android would call the method automatically, why when remove the adapter.notifyDataSetChanged();
the UI couldn't refresh?
The point is that you are entering data in an Order i.e. when you pushed an item in the list it goes all the way down , u didn't enter it at 4th , 5th any random index so you don't have to call notifyDataSetChanged()
as its definition itself says : that some data has been changed or any View reflecting the data set should refresh itself to make the new data visible on the list, as in this case data is going out of the scope of the visible list i.e. the number of child being shown in the list so it(the ListView) always calls for the next views after that particular last shown index's value (item) in the listView.!
Hope i made it a bit clear to you...!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Answer after you edited the question, after searching and having my own thoughts and understandings in this regards :
In first case there was no change needed to be made for the VIEW.. Right.? If this point is clear to you then come on the second. In first, you fetched data from the DB and then added an item in the list then Populate it after setting the adapter.!! Adapter wasn't set until the whole method executes and completes the List..! OK.
But in the second scenario you are changing the VIEW (pretty much everything is View in android) so u are manipulating a view by adding another item on the already populated View (which has already a set-ted adapter), so this time u need to tell the view that hey , i have added an item in you now refresh yourself then update the List (i.e. Display).