Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 530
Yriuns Avatar asked Jan 16 '16 12:01

Yriuns


People also ask

What does adapter notifyDataSetChanged () do?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

How do you call notifyDataSetChanged outside adapter?

If you are accessing it from outside scope, you can get it from the ListView object and cast it so you can call notifyDataSetChanged(). Like so: ServerItemAdapter listAdapter = (ServerItemAdapter) listView. getAdapter(); listAdapter.

How do I use notifyDataSetChanged in activity?

Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure. Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity. Then, notifyDataSetChanged() will work.

What is Notifydatachange?

notifyDataSetChanged tells the recycler view that every single item should be updated. However, its usually called when only 1 item has changed. Our RecyclerView. Adapter s should be changed to only notify that the changed items should be updated.


1 Answers

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

like image 180
Lilylakshi Avatar answered Oct 19 '22 05:10

Lilylakshi