Reputation: 359
I am trying to update the content of my listview by adding stuff to it. Although the listview does update its contents the size still stays the same for some reason. So for example, if the original listview contains A, B, Y, Z and I add C and D to it, the updated list view will be: A, B, C, D. What am I doing wrong?
Here is some relavent code:
//in main activity...
//additionalSongs is an arraylist
addAdditionalSongs(additionalSongs);//add the additional songs to the main list
songTabFragment = new SongTabFragment();//update the list on the screen
...
private void addAdditionalSongs(ArrayList<Song> additionalSongs){
for(int i = 0; i < additionalSongs.size(); i++) {
songList.add(additionalSongs.get(i));
}
}
SongTabFragment class
public class SongTabFragment extends Fragment {
private ListView songView;
private Context context;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.song_tab_layout, container, false);
songView = (ListView) rootView.findViewById(R.id.song_list); //get a reference to the ListView created in song_tab_layout
SongAdapter theAdapter = new SongAdapter(context, MainActivity.getSongArray());
songView.setAdapter(theAdapter); //pass the ListView object the appropriate adapter
return rootView;
}
}
SongAdapter class
public class SongAdapter extends BaseAdapter {
private ArrayList<Song> songArray;
private LayoutInflater songInf;
public SongAdapter(Context c, ArrayList<Song> grabbedSongArray){
songArray = grabbedSongArray;
songInf = LayoutInflater.from(c);
}
@Override
public int getCount() {
return songArray.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout listLayout = (LinearLayout)songInf.inflate(R.layout.song, parent, false);
//layout for each individual song in the list. Uses song.xml
TextView songView = (TextView)listLayout.findViewById(R.id.song_title);
TextView artistView = (TextView)listLayout.findViewById(R.id.song_artist);
Song currentSong = songArray.get(position);
songView.setText(currentSong.getTitle()); //pass data to textView objects in each list item
artistView.setText(currentSong.getArtist());
listLayout.setTag(position); //use the song's position in list as a tag
return listLayout;
}
}
Upvotes: 0
Views: 79
Reputation: 543
It might be that the SongTabFragment is not being updated. Instead of accessing your song array via the MainActivity
MainActivity.getSongArray()
Why not add a method in your fragment to update the arraylist in the SongAdapter and then notify the list view that the data set has changed so that it will recreate the view based on the new array list.
Example
In fragment class
// Fragment code
public void updateAdapterArray(ArrayList<Songs> adapter) {
((SongAdapter) mListView.getAdapter()).setSongs(adapter);
}
In adapter class
//Adapter code
public void setSongs(ArrayList<Songs> adapter) {
this.songList = adapter;
notifyDataSetChanged();
}
In mainactivity
// your mainactivity code
SongTabFragment songFragment = (SongTabFragment) mFragmentManager.findFragmentById(R.id.fragContainer);
songFragment.updateAdapterArray(newSongList);
Upvotes: 1
Reputation: 522
Are you updating the item count in your list view? If the listview still only thinks there are 4 items in the list it will only display 4 items. You have to update the value the getCount() returns.
Upvotes: 0
Reputation: 2493
Check your getCount()
method,
@Override
public int getCount() {
return list.getSize(); //it should return size of list. Not 4
}
Upvotes: 0