简体   繁体   中英

update the text of a listView row

I want my listView to be updated after clicking on a row (or any event, but let's focus on click).

I did something, but it updates more than one row (maybe it updates the first visible row and the one after the last visible...). Here is the full code

Activity code

DatabaseHandler colisageBase;
ListView listView;
List<Site> sites;
String id_tournee;
SiteAdapter siteAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_site_choice);
    Intent intent = getIntent();
    id_tournee = intent.getStringExtra("idTourneeSelectionnee");
    this.listView = findViewById(R.id.list_view_site);
    this.colisageBase = new DatabaseHandler(this);
    sites = colisageBase.selectAllSite(id_tournee);
    siteAdapter = new SiteAdapter(SiteChoiceActivity.this, sites);
    listView.setAdapter(siteAdapter);
    colisageBase.closeDB();

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Site selectedSite = sites.get(position);
            selectedSite.setIsBarred(true);
            sites.set(position, selectedSite);
            siteAdapter.notifyDataSetChanged();
            //goToOperationActivity(selectedSite.SiteOut());
        }
    });

Adapter code

public class SiteAdapter extends ArrayAdapter<Site> {

public SiteAdapter(Context context, List<Site> sites) {
    super(context, 0, sites);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_site,parent, false);
    }

    SiteViewHolder viewHolder = (SiteViewHolder) convertView.getTag();
    if(viewHolder == null){
        viewHolder = new SiteViewHolder();
        viewHolder.heure_supposee = convertView.findViewById(R.id.heure_supposee);
        viewHolder.libelle_site = convertView.findViewById(R.id.libelle_site);
        viewHolder.logo_telephone = convertView.findViewById(R.id.logo_phone);
        convertView.setTag(viewHolder);
    }

    Site site = getItem(position);

    viewHolder.heure_supposee.setText(site.getHeure_supposee());
    viewHolder.libelle_site.setText(site.getLibelle_site());
    viewHolder.logo_telephone.setVisibility(View.INVISIBLE);
    if (site.getSur_appel().equals("O")) viewHolder.logo_telephone.setVisibility(View.VISIBLE);
    if (site.isBarred()) viewHolder.libelle_site.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

    return convertView;
}

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

private class SiteViewHolder{
    public TextView heure_supposee;
    public TextView libelle_site;
    public ImageView logo_telephone;
}

}

Please suggest what's wrong with the code.

The answer was given in the comments by I_A_Mok, but i have to add more details:

In the case of a cell, when you do an action in an "if" condition , you usually have to do the opposite in an "else" condition.

In my case, after my condition where I strike through text, I had to add an else condition where I don't strike through text.

if (site.isBarred()){
        viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }else {
        viewHolder.libelle_site.setPaintFlags(viewHolder.libelle_site.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM