OnRestart活动,坏项目在列表视图中突出显示

时间:2012-04-05 17:38:10

标签: android listview highlight

我的listview是使用relativeLayout创建的。当我触摸" texViewLibelleListe"时,使用OnClick方法突出显示listview的项目(在" bleu_c"中)并启动一个新活动(" ActiviteSoldeAchat") 。当我销毁此活动时(" ActiviteSoldeAchat"),第一个活动重新开始。

我的问题:当我重新启动活动或突出显示好项目时,我想禁用列表视图中所有项目的突出显示。我怎样才能做到这一点? 此时,这不是突出显示的好项目。有什么想法吗?

感谢您的帮助

我找到的唯一解决方案是在OnResume方法中完全重新加载listview,如下所示:      adapter = new ListeAdapter(this,listeRepository.GetAll());      ListListe.setAdapter(适配器);

但我认为,这不是一个干净的解决方案

主要活动:

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

为列表视图提供支持:

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

}

1 个答案:

答案 0 :(得分:0)

请尝试以下内容,而不是重新加载总列表视图。

@Override
public void onResume() {
    super.onResume();
    adapter.notifyDataSetChanged();
}