【问题标题】:Checkbox.checked is not working in View MonodroidCheckbox.checked 在 View Monodroid 中不起作用
【发布时间】:2013-06-10 23:42:06
【问题描述】:

我是 Android 单声道的新手。我正在尝试制作一个简单的复选框列表,我可以在其中选择我的候选人,按一个按钮将它们插入数据库。到这里为止一切都很好,但问题在于加载我的检查值。即使我明确输入 this.Checked = true;

,我的复选框也始终未选中

这是我的代码:

加载列表视图:

    var CandidatesList = FindViewById<ListView>(Resource.Id.lstVotingCandidates);
    CandidatesVoteAdapter cva = new CandidatesVoteAdapter(this, MAE.Code.Utilities.CandidatesInfo.CurrentList);
    CandidatesList.ChoiceMode = ChoiceMode.Multiple;
    CandidatesList.Adapter = cva;

适配器:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    View view = convertView; // re-use an existing view, if one is available
    if (view == null) // otherwise create a new one
    {  
       view = new Views.CandidateVoteItemView(context, items[position]);
    }
    return view;
}

查看:

class CandidateVoteItemView : CheckBox
{
    public MAE.MAEService.Candidate Candidate { get; protected set; }
    public CandidateVoteItemView(Context context, MAE.MAEService.Candidate candidate) : base(context)
    {
        this.Text = candidate.FirstName;                                 //Working 
        this.Checked = true;// (candidate.isChecked == 1) ? true : false;//Not Working
    }
}

【问题讨论】:

  • 你可以试试这个方法:lv.SetItemChecked(1, true); Taken from official xamarin site 并检查第 2.2 节
  • 感谢您的回答,它完成了这项工作,即使它应该按照我最初的方式工作,因为我的视图只是一个复选框。无论如何,非常感谢
  • 没问题,很高兴我能帮上忙。
  • @jHogen 请将其作为答案发布,以便 OP 接受它,以便你们都获得积分。
  • @Cheesebaron 感谢您的提示,添加了答案。

标签: c# android xamarin.android xamarin


【解决方案1】:

你可以试试这个方法:

lv.SetItemChecked(1, true);

Taken from official xamarin site 并查看第 2.2 节

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2016-02-03
    • 2019-01-06
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多