My listview is created with a relativeLayout. When I touch the "texViewLibelleListe", the item of the listview is highlighted (in "bleu_c") with the OnClick method and start a new activity ("ActiviteSoldeAchat"). When I destroy this activity ("ActiviteSoldeAchat"), the first activity restart.
My question : I want to disable the highlight of a all item in the listview when I restart the activity or highlight the good item. How can I do this? At this time, this is not the good item that is highlighted. Any idea?
Thank you for your help
The only one solution I found is to reload entirely the listview in the OnResume method like this : adapter = new ListeAdapter(this, listeRepository.GetAll()); ListListe.setAdapter(adapter);
The main activity :
public class ActiviteSoldeListe extends Activity {
ListView ListListe;
Button boutonAjouterListe;
private ListeAdapter adapter;
private ListeRepository listeRepository=new ListeRepository(this);;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soldeliste);
//On récupère tous les éléments de notre interface graphique grâce aux ID
ListListe = (ListView) findViewById(R.id.listListe);
// ouverture de la base
listeRepository.Open();
// récupération de tous les éléments de la table achat et adaptateur
adapter = new ListeAdapter(this, listeRepository.GetAll());
// fermeture de la base
listeRepository.Close();
ListListe.setAdapter(adapter);
}
//au lancement de l'activité
protected void onStart(){
super.onStart();
}
protected void onRestart(){
super.onRestart();
}
protected void onResume(){
super.onResume();
UpdateAdapter();
}
private void UpdateAdapter() {
listeRepository.Open();
adapter.setListes(listeRepository.GetAll());
listeRepository.Close();
adapter.notifyDataSetChanged();
}
Tha adpater for the listview :
public class ListeAdapter extends BaseAdapter implements OnClickListener{
private List<Liste> listeListe;
private LayoutInflater inflater;
private Context context;
public int confirmDelete;
public void setListes(List<Liste> listeListe) {
this.listeListe = listeListe;
}
public ListeAdapter(Context context, List<Liste> listeListe) {
this.listeListe = listeListe;
this.setContext(context);
this.inflater = LayoutInflater.from(context);
}
public int getCount() {
return listeListe.size();
}
public Object getItem(int position) {
return listeListe.get(position);
}
public long getItemId(int position) {
return listeListe.get(position).getId();
}
private class ViewHolder {
public TextView tvLibelleListe;
public TextView tvDate;
public Button btSupprimer;
}
public View getView(final int position, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.layout_item_liste, null);
holder.tvLibelleListe = (TextView) view
.findViewById(R.id.textViewLibelleListe);
holder.tvDate = (TextView) view
.findViewById(R.id.textViewDate);
holder.btSupprimer = (Button) view
.findViewById(R.id.buttonSupprimerListe);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
final Integer IdListe = listeListe.get(position).getId();
final RelativeLayout listlayout = (RelativeLayout) view.findViewById(R.id.ListViewListe);
holder.tvLibelleListe.setText(listeListe.get(position).getLibelleListe() + " - " + listeListe.get(position).getouvert());
holder.tvDate.setText(listeListe.get(position).getdate() + "");
holder.btSupprimer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ConfirmDelete(getContext(), IdListe);
}
});
holder.tvLibelleListe.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listlayout.setBackgroundResource(R.color.bleu_f);
ActiviteSoldeListe soldeListeActivity = (ActiviteSoldeListe) getContext();
Intent intent = new Intent();
intent.setClass(soldeListeActivity, ActiviteSoldeAchat.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("IdListe",IdListe);
soldeListeActivity.startActivity(intent);
}
});
holder.tvDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listlayout.setBackgroundResource(R.color.bleu_f);
ActiviteSoldeListe soldeListeActivity = (ActiviteSoldeListe) getContext();
Intent intent = new Intent();
intent.setClass(soldeListeActivity, ActiviteSoldeAchat.class);
intent.putExtra("IdListe",IdListe);
soldeListeActivity.startActivity(intent);
}
});
return view;
}
}
Instead of reloading total list view try the below..
@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}