OnRestart Activity, bad item is highlighted in a listview

Go To StackoverFlow.com

0

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

But I think, it's not a clean solution

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

}

2012-04-05 17:38
by user1298799
what exactkly you are trying to d - Shankar Agarwal 2012-04-05 17:49
@Agarwal : When I return in my activity with my listview , I do not want that an item is highlighte - user1298799 2012-04-05 19:06
Then set the background back to what it was in the 'onResume()' method - Urban 2012-04-05 19:23
Paste this line in on restart method listlayout.setBackgroundResource(R.color.new color) - Shankar Agarwal 2012-04-06 01:43
@Agarwal : your solution don't work. Because I always have another item that is higlighted by the color "bleu_c" - user1298799 2012-04-06 07:27


0

Instead of reloading total list view try the below..

@Override
public void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}
2012-04-06 07:47
by Shankar Agarwal
I already have the notifyDataSetChanged. The problem came from the fact that, when I return on the listview, it's another item of the listview that is highlighted - user1298799 2012-04-06 08:58
better to paste your cod - Shankar Agarwal 2012-04-06 09:22
I have added the code. Thanks for your hel - user1298799 2012-04-06 10:34
Ads