简体   繁体   English

ListView在Android中无法正确更新

[英]ListView not updating properly in Android

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. 尽管listview确实更新了其内容,但由于某种原因,其大小仍然保持不变。 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? 因此,例如,如果原始列表视图包含A,B,Y,Z,并且我向其中添加了C和D,则更新后的列表视图将为:A,B,C,D。我在做什么错了?

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

It might be that the SongTabFragment is not being updated. SongTabFragment可能没有被更新。 Instead of accessing your song array via the MainActivity 而不是通过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. 为什么不在片段中添加一种方法来更新SongAdapter中的arraylist,然后通知列表视图数据集已更改,以便它将根据新的数组列表重新创建视图。

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

Check your getCount() method, 检查您的getCount()方法,

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

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. 如果列表视图仍然仅认为列表中有4个项目,它将仅显示4个项目。 You have to update the value the getCount() returns. 您必须更新getCount()返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM