Question

I have a listview and I have to add two more element in listview. I have to add notifyDataSetChanged() . but not sure how to do it.

listView = (ListView) findViewById(R.id.my_listview);
myListAdapter = new MyListAdapter (this, sourceList);
listView.setAdapter(myListAdapter);
myListAdapter.notifyDataSetChanged();

sourceList is the List .

I need to add 2 items at the end of the Listview. Thanks.

Was it helpful?

Solution

Just add your desired data to the adapter. For example:

myListAdapter.add(obj1);

The newly added data will be attached at the end of your ListView.

Remember: when injecting data directly into an Adapter, you don't need to call its notifyDataSetChanged method.

If you modify your sourceList object, in that case its necessary to call notifyDataSetChanged method in order for the Adapter to refresh its contents.

OTHER TIPS

Check out this Dummy Example to add the adapter to the List.

Read the Comment

ListView lstViewtodo; //Your ListView

List<NoteModel> list_note_model = new ArrayList<NoteModel>(); // for Adding the Array of your Model 

NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false); //Your Model

list_note_model.add(0, noteModel); //Adding the value to array

lstViewtodo.setAdapter(list); //finally adding the content to list

To Add the Content Dynamically...

NoteListAdapter list = new NoteListAdapter( NoteMainActivity.this , R.layout.todo_adapter_view , list_note_model); //Your class extending the ArrayAdapter<NoteModel>

    NoteModel noteModel = new NoteModel(txttitle, jsonArray, obj.getUid(), txtColorType, false);'

    list_note_model.add(0, noteModel);

list.notifyDataSetChanged();

Let me Know if You Difficulty...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top