Why do I not need to use Adapter.notifyDataSetChanged()?
Asked Answered
L

3

9

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?

Leotie answered 16/1, 2016 at 12:4 Comment(0)
T
0

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).

Thermometer answered 16/1, 2016 at 19:1 Comment(2)
thanks! but i have a new question, refresh in the above:)Leotie
@HungryMilk hi there have a look on the updated explanation and if it helps accept it as an answer :)Thermometer
P
1

That method is called internally in the Android framework. You do not have to call it explicitly.

source: https://www.udacity.com/course/developing-android-apps--ud853

Percolate answered 16/1, 2016 at 12:48 Comment(3)
I mean when i set the adapter, the list is empty. And after i used readcontacts, the list became not empty, but i didn't use notifydatachanged()to refresh it. so why the listview still can show info?Leotie
@HungryMilk When the contents of the list is changed, Android framework automatically calls notifyDataChanged(). So you don't have to call it yourself.Percolate
thanks! but i have a new question, refresh in the above:)Leotie
A
0

In addition to Lily's answer, if you write your own adapter, you have to call it explicitly.

Annulment answered 16/1, 2016 at 18:43 Comment(0)
T
0

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).

Thermometer answered 16/1, 2016 at 19:1 Comment(2)
thanks! but i have a new question, refresh in the above:)Leotie
@HungryMilk hi there have a look on the updated explanation and if it helps accept it as an answer :)Thermometer

© 2022 - 2024 — McMap. All rights reserved.