I have a ArrayAdapter
in a Listview
and when the user clicks and holds it it displays a Contextmenu
with the Delete the favorite option.
The delete function is working, but since I create the Adapter
in the onCreateView
every time I drop the landscape the items come back because it recreates the View
I would like to know how to avoid this
Fragment displaying the bookmarks
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_item, container, false);
mAdapter = new FavoritosAdapter(getActivity(), carregarFavoritos());
listView = (ListView) rootView.findViewById(R.id.list_favoritos);
listView.setAdapter(mAdapter);
registerForContextMenu(listView);
listView.setEmptyView(rootView.findViewById(R.id.empty));
return rootView;
}
public ArrayList<Favoritos> carregarFavoritos(){
ArrayList<Favoritos> favoritos = new ArrayList<Favoritos>();
favoritos.add(new Favoritos("endereco", 5, false, 7, 22));
favoritos.add(new Favoritos("endereco", 3.5 , true, 7, 22));
favoritos.add(new Favoritos("endereco", 2, false, 7, 22));
favoritos.add(new Favoritos("endereco", 4.5, true, 7, 22));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.list_favoritos) {
//Outras opções
menu.add(Menu.NONE, 3, Menu.NONE, R.string.txt_excluir);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
//Função das outras opções
case 3:
favoritosExcluidos = favoritos.remove(info.position);
mAdapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar.make(listView, R.string.txt_favorito_excluido, Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.txt_desfazer, new View.OnClickListener() {
@Override
public void onClick(View view) {
Favoritos favorito = favoritosExcluidos;
favoritos.add(info.position, favorito);
mAdapter.notifyDataSetChanged();
}
});
snackbar.show();
break;
}
return true;
}
FavoritesAdapter
public FavoritosAdapter(Context context, ArrayList<Favorito> favoritos){
super(context, 0, favoritos);
}
public static class ItemViewHolder{
TextView textView1;
TextView textView2;
TextView textView3;
TextView textView4;
TextView textView5;
RatingBar ratingBar;
ImageView imageView;
TextView textView6;
public ItemViewHolder(View view){
textView1 = (TextView) view.findViewById(R.id.txt1);
textView2= (TextView) view.findViewById(R.id.txt2);
textView3= (TextView) view.findViewById(R.id.txt3);
textView4= (TextView) view.findViewById(txt4);
textView5= (TextView) view.findViewById(txt5);
ratingBar= (RatingBar) view.findViewById(R.id.ratingBar);
imageView= (ImageView) view.findViewById(R.id.img);
textView6= (TextView) view.findViewById(R.id.txt6);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.favoritos_item, parent, false);
}
Favorito currentFavorito = getItem(position);
final ItemViewHolder holder;
if(listItemView.getTag() == null){
holder = new ItemViewHolder(listItemView);
listItemView.setTag(holder);
} else {
holder = (ItemViewHolder) listItemView.getTag();
}
if(currentFavorito.getBooleanTxt6()){
holder.txt6.setText(currentFavorito.txt1());
} else {
holder.txt6.setText(currentFavorito.getTxt1());
}
holder.txt1.setText(currentFavorito.getTxt1());
holder.txt2.setText(currentFavorito.getTxt2());
holder.txt3.setText(currentFavorito.getTxt3());
holder.txt4.setText(currentFavorito.getTxt4());
holder.txt5.setText(currentFavorito.getTxt5());
holder.ratingBar.setRating(currentFavorito.getAvaliacao());
if(currentFavorito.getBooleanTxt6()){
holder.imageView.setImageResource(R.drawable.img);
holder.txt6.setText(R.string.txt6true);
holder.txt6.setTextColor(getContext().getResources().getColor(R.color.verde));
} else {
holder.imageView.setImageResource(R.drawable.img2);
holder.txt6.setText(R.string.txt6false);
holder.txt6.setTextColor(getContext().getResources().getColor(R.color.vermelho));
}
return listItemView;
}