ListView在Android

时间:2016-01-27 21:30:16

标签: android listview

我正在尝试通过向其添加内容来更新listview的内容。尽管listview确实更新了其内容,但由于某种原因,大小仍然保持不变。因此,例如,如果原始列表视图包含A,B,Y,Z和I,则向其添加C和D,更新后的列表视图将为:A,B,C,D。我做错了什么?

以下是一些相关代码:

//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类

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类

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;
    }
}

3 个答案:

答案 0 :(得分:1)

可能是SongTabFragment没有更新。而不是通过MainActivity访问您的歌曲阵列

MainActivity.getSongArray()

为什么不在片段中添加一个方法来更新SongAdapter中的arraylist,然后通知列表视图数据集已更改,以便它将根据新的数组列表重新创建视图。

实施例

在片段类

// Fragment code 
public void updateAdapterArray(ArrayList<Songs> adapter) {
       ((SongAdapter) mListView.getAdapter()).setSongs(adapter);
}

在适配器类

//Adapter code
public void setSongs(ArrayList<Songs> adapter) {
    this.songList = adapter;
    notifyDataSetChanged();
}

在主要活动中

// your mainactivity code
SongTabFragment songFragment = (SongTabFragment) mFragmentManager.findFragmentById(R.id.fragContainer);
songFragment.updateAdapterArray(newSongList);

答案 1 :(得分:0)

检查您的getCount()方法,

@Override
public int getCount() {
    return list.getSize(); //it should return size of list. Not 4
}

答案 2 :(得分:0)

您是否在列表视图中更新项目计数?如果列表视图仍然只认为列表中有4个项目,它将只显示4个项目。您必须更新getCount()返回的值。