更新listView行的文本

时间:2018-05-31 08:02:07

标签: java android listview android-arrayadapter

我希望在点击一行(或任何事件,但让我们专注于点击)后更新我的listView。

我做了一些事情,但它更新了多行(也许它会更新第一个可见行和最后一个可见行之后的那一行......)。 这是完整的代码

活动代码

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

适配器代码

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

}

请说明代码有什么问题。

1 个答案:

答案 0 :(得分:0)

答案在I_A_Mok的评论中给出,但我必须添加更多细节:

对于单元格,当您在“if”条件下执行操作时,通常必须在“else”条件下执行相反操作。

在我的情况下,在我查看文本的情况之后,我不得不添加一个其他条件,我不会通过文本。

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