Reputation: 1
When I open a new activity from my listview activity and then go back to listview activity or if I reopen that same listview actvity , my listview items gets repeated.
public class PlaylistAdapter extends ArrayAdapter<PlaylistBean> {
Context context;
private LayoutInflater inflater;
public static class ViewHolder
{
public final TextView name;
public final ImageView image;
public ViewHolder(View view)
{
name = (TextView) view.findViewById(R.id.namePlaylist);
image = (ImageView) view.findViewById(R.id.imagePlaylist);
}
}
public PlaylistAdapter(Context context, List<PlaylistBean> items)
{
super(context, R.layout.row_layout_playlist, items);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = LayoutInflater.from(getContext());
view = vi.inflate(R.layout.row_layout_playlist, null);
}
final ViewHolder viewHolder = new ViewHolder(view);
PlaylistBean playlistBean = getItem(position);
viewHolder.name.setText(playlistBean.getName());
viewHolder.image.setImageResource(playlistBean.getImage());
return view;
}
}
Here is my code for PlaylistBean class
public class PlaylistBean implements Serializable {
private int image;
private String name;
public PlaylistBean(int image, String name)
{
this.image = image;
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is the code for my main activity
public class Playlist extends AppCompatActivity {
public static List<PlaylistBean> items = new ArrayList<>();
ArrayAdapter<PlaylistBean> myArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
playlistListView = (ListView) findViewById(R.id.playlistListView);
myArrayAdapter = new PlaylistAdapter(Playlist.this, items);
playlistListView.setAdapter(myArrayAdapter);
createData();
}
public void createData()
{
PlaylistBean bean1 = new PlaylistBean(R.drawable.popular_upload, "Popular Uploads");
items.add(bean1);
}
}
Upvotes: 0
Views: 63
Reputation: 762
This is what happens.
List<PlaylistBean> items
is static.ListView
ListView
gets empty as there the items.size()
is 0createData()
method is called which fill list.How to over come this.
I hope that you want to display something in the list view at the first run and you want to keep the items
list static
@Override
protected void onCreate(Bundle sis){
super.onCreate(sis);
setContentView(R.layout.activity_playlist);
playlistListView = (ListView) findViewById(R.id.playlistListView);
//fill the list here.
createData();
myArrayAdapter = new PlaylistAdapter(Playlist.this, items);
playlistListView.setAdapter(myArrayAdapter);
}
Then change your createData()
method.
public static void createData(){
//instatiate the list items here
items=new ArrayList<>();
PlaylistBean bean1 = new PlaylistBean(R.drawable.popular_upload, "Popular Uploads");
items.add(bean1);
}
Upvotes: 0
Reputation: 16277
When your activity goes to background, you can clear your listview with below code:
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
yourListView.setListAdapter(null); //This clears the listview items
}
Upvotes: 2