ListView android xamarin中的数据随机播放

时间:2015-02-27 08:07:36

标签: c# android android-listview xamarin xamarin.android

我正在使用android xamarin中的listview。我用webview,radiogroup和3个单选按钮创建了一个自定义控件。我有一个html字符串,它被拆分以创建一个问题数组,并传递给适配器。它们将它们绑定到ListView,如屏幕截图所示。

问题是每当我滚动屏幕时,数据都会混乱,行会混乱。假设我有5个问题,按顺序A,B,C,D,E - 它可以随机变成D,B,C,D,E或E,B,C,D,E或A B E D C.不确定为什么这应该发生。尝试了一些事情,但它只是赢了工作。

我正在使用片段作为屏幕,因为我还有其他屏幕更新数据对象,后来提交给服务器。

enter image description here

我确信Adapter类中的代码有问题,但无法弄清楚。这是代码(**注释代码是我尝试过的旧代码。)

更新了GetView代码

public override View GetView (int position, View convertView, ViewGroup parent)
            {
                //var view = convertView ?? _activity.Activity.LayoutInflater.Inflate (Resource.Layout.SegmentedControl, parent, false);
                View view = convertView;
                ViewHolder holder = null;

                Utils.WriteDebugInfo ("position - " + position + " dsadsadsadad - " + _questionList [position]);
                //if (view == null) {
                /*if (view != null) {
                    view = null;
                }*/
                view = _activity.Activity.LayoutInflater.Inflate (Resource.Layout.SegmentedControl, parent, false);
                holder = new ViewHolder ();

                holder.WebViewQuestion = view.FindViewById<WebView> (Resource.Id.segmentControlQuestionTextView);
                //holder.RadioGroupToUse = view.FindViewById<RadioGroup> (Resource.Id.segmentControlRadioGroupLayout);
                holder.RadioButton1 = view.FindViewById<Button> (Resource.Id.segmentControlRadio1);
                holder.RadioButton2 = view.FindViewById<Button> (Resource.Id.segmentControlRadio2);
                holder.RadioButton3 = view.FindViewById<Button> (Resource.Id.segmentControlRadio3);

                //holder.RadioButton1.CheckedChange += ((object sender, CompoundButton.CheckedChangeEventArgs e) => {
                holder.RadioButton1.Click += ((object sender, EventArgs e) => {
                    //if ((sender as RadioButton).Checked) {
                    //Utils.WriteDebugInfo ("Choice is : A ==== " + _questionArr [position]);
                    _activity.Activity.RunOnUiThread (() => {
                        holder.RadioButton2.SetBackgroundResource (0);
                        holder.RadioButton2.SetBackgroundColor (new Android.Graphics.Color(255,255,0,255));

                        holder.RadioButton3.SetBackgroundResource (0);
                        holder.RadioButton3.SetBackgroundColor (new Android.Graphics.Color(255,0,0,255));

                        //(sender as Button).SetBackgroundColor(Android.Graphics.Color.LimeGreen);
                        (sender as Button).SetBackgroundResource (Resource.Drawable.GreenButton_BlackBorder);

                    });
                    Utils.WriteDebugInfo ("Choice is : A ==== " + _questionList [position].QuestionText);
                    UpdateFeedback2Answer3String (position, 0, false);
                    //}
                });

                //holder.RadioButton2.CheckedChange += ((object sender, CompoundButton.CheckedChangeEventArgs e) => {
                holder.RadioButton2.Click += ((object sender, EventArgs e) => {
                    //if ((sender as RadioButton).Checked) {
                    //Utils.WriteDebugInfo ("Choice is : B === " + _questionArr [position]);
                    _activity.Activity.RunOnUiThread (() => {
                        holder.RadioButton1.SetBackgroundResource (0);
                        holder.RadioButton1.SetBackgroundColor (new Android.Graphics.Color(0,255,0,255));

                        holder.RadioButton3.SetBackgroundResource (0);
                        holder.RadioButton3.SetBackgroundColor (new Android.Graphics.Color(255,0,0,255));

                        //(sender as Button).SetBackgroundColor(Android.Graphics.Color.Yellow);
                        (sender as Button).SetBackgroundResource (Resource.Drawable.YellowButton_BlackBorder);

                    });
                    Utils.WriteDebugInfo ("Choice is : A ==== " + _questionList [position].QuestionText);
                    UpdateFeedback2Answer3String (position, 1, false);
                    //}
                });

                //holder.RadioButton3.Click += ((object sender, CompoundButton.CheckedChangeEventArgs e) => {
                holder.RadioButton3.Click += ((object sender, EventArgs e) => {
                    //if ((sender as RadioButton).Checked) {
                    //Utils.WriteDebugInfo ("Choice is : C === " + _questionArr [position]);
                    _activity.Activity.RunOnUiThread (() => {
                        holder.RadioButton1.SetBackgroundResource (0);
                        holder.RadioButton1.SetBackgroundColor (new Android.Graphics.Color(0,255,0,255));

                        holder.RadioButton2.SetBackgroundResource (0);
                        holder.RadioButton2.SetBackgroundColor (new Android.Graphics.Color(255,255,0,255));

                        //(sender as Button).SetBackgroundColor(Android.Graphics.Color.Red);
                        (sender as Button).SetBackgroundResource (Resource.Drawable.RedButton_BlackBorder);

                    });
                    Utils.WriteDebugInfo ("Choice is : A ==== " + _questionList [position].QuestionText);
                    UpdateFeedback2Answer3String (position, 2, false);
                    //}
                });
                view.Tag = holder;
                //segmentControlQuestionTextView = view.FindViewById<WebView> (Resource.Id.segmentControlQuestionTextView);

                /*} else {
                    view = convertView;
                    holder = view.Tag as ViewHolder;

                }*/

                string html_string;
                html_string = "<html>" +
                "<head>" +
                "</head>" +
                "<body style='background-color: #50b983'>" +
                "<font face='ProximaNova_Regular' size='4' color='white'>{text}</font> " +
                "</body>" +
                "</html>"; 
                //html_string = html_string.Replace ("{text}", _questionArr [position]);
                Utils.WriteDebugInfo ("Current View : " + position);
                html_string = html_string.Replace ("{text}", _questionList [position].QuestionText);
                holder.WebViewQuestion.LoadData (html_string, "text/html; charset=UTF-8", null);
                string feedback2Question3Answer = string.Empty;

                if (ChallengeFeedbackActivity.ReviewState) {
                    if (ChallengeFeedbackActivity.FeedbackToReview != null && ChallengeFeedbackActivity.FeedbackToReview != null) {
                        //SetFragmentData (ChallengeFeedbackActivity.FeedbackToReview.UserChallengeFeedbackResponse.Feedback2Question3Answer, position);
                        feedback2Question3Answer = ChallengeFeedbackActivity.FeedbackToReview.UserChallengeFeedbackResponse.Feedback2Question3Answer;
                    }
                } else {
                    if (AppData.Instance.FeedbackSubmissionData != null) {
                        feedback2Question3Answer =  Utils.IsStringNullOrEmptyOrWhiteSpace(AppData.Instance.FeedbackSubmissionData.Feedback2Question3Answer)
                            ?FeedbackSegmentedQuestionsFragment.Feedback2Answer3String:AppData.Instance.FeedbackSubmissionData.Feedback2Question3Answer;
                        //SetFragmentData (AppData.Instance.FeedbackSubmissionData.Feedback2Question3Answer, position);
                    } else {
                        feedback2Question3Answer = FeedbackSegmentedQuestionsFragment.Feedback2Answer3String;

                    }
                }


                if (!Utils.IsStringNullOrEmptyOrWhiteSpace (feedback2Question3Answer)) {
                    string[] ansarr = new string[feedback2Question3Answer.Split (',').Length];
                    ansarr = feedback2Question3Answer.Split (',');
                    switch (ansarr [position].Split (':') [1].ToLower ()) {
                    case "a":
                        _activity.Activity.RunOnUiThread (() => {
                            holder.RadioButton1.SetBackgroundResource (Resource.Drawable.GreenButton_BlackBorder);
                            //holder.RadioButton1.SetBackgroundColor (Android.Graphics.Color.LimeGreen);
                            holder.RadioButton2.SetBackgroundResource (0);
                            holder.RadioButton2.SetBackgroundColor (new Android.Graphics.Color(255,255,0,255));
                            holder.RadioButton3.SetBackgroundResource (0);
                            holder.RadioButton3.SetBackgroundColor (new Android.Graphics.Color(255,0,0,255));

                        });

                        //holder.RadioButton1.Checked = true;
                        //holder.RadioButton2.Checked = false;
                        //holder.RadioButton3.Checked = false;

                        break;
                    case "b":
                        _activity.Activity.RunOnUiThread (() => {
                            holder.RadioButton2.SetBackgroundResource (Resource.Drawable.YellowButton_BlackBorder);
                            //holder.RadioButton2.SetBackgroundColor (Android.Graphics.Color.Yellow);
                            holder.RadioButton1.SetBackgroundResource (0);
                            holder.RadioButton1.SetBackgroundColor (new Android.Graphics.Color(0,255,0,255));
                            holder.RadioButton3.SetBackgroundResource (0);
                            holder.RadioButton3.SetBackgroundColor (new Android.Graphics.Color(255,0,0,255));

                        });

                        //holder.RadioButton1.Checked = false;
                        //holder.RadioButton2.Checked = true;
                        //holder.RadioButton3.Checked = false;
                        break;
                    case "c":
                        _activity.Activity.RunOnUiThread (() => {
                            holder.RadioButton3.SetBackgroundResource (Resource.Drawable.RedButton_BlackBorder);
                            //holder.RadioButton3.SetBackgroundColor (Android.Graphics.Color.Red);
                            holder.RadioButton1.SetBackgroundResource (0);
                            holder.RadioButton1.SetBackgroundColor (new Android.Graphics.Color(0,255,0,255));
                            holder.RadioButton2.SetBackgroundResource (0);
                            holder.RadioButton2.SetBackgroundColor (new Android.Graphics.Color(255,255,0,255));

                        });

                        //holder.RadioButton1.Checked = false;
                        //holder.RadioButton2.Checked = false;
                        //holder.RadioButton3.Checked = true;
                        break;
                    default:
                        holder.RadioButton2.SetBackgroundResource (Resource.Drawable.YellowButton_BlackBorder);
                        //holder.RadioButton2.SetBackgroundColor (Android.Graphics.Color.Yellow);
                        holder.RadioButton1.SetBackgroundResource (0);
                        holder.RadioButton1.SetBackgroundColor (new Android.Graphics.Color(0,255,0,255));
                        holder.RadioButton3.SetBackgroundResource (0);
                        holder.RadioButton3.SetBackgroundColor (new Android.Graphics.Color(255,0,0,255));
                        //holder.RadioButton1.Checked = false;
                        //holder.RadioButton2.Checked = true;
                        //holder.RadioButton3.Checked = false;
                        break;
                    }

                    if (ChallengeFeedbackActivity.ReviewState) {
                        holder.RadioButton1.Enabled = false;
                        holder.RadioButton2.Enabled = false;
                        holder.RadioButton3.Enabled = false;

                    }

                }

                return view;
            }

        }

1 个答案:

答案 0 :(得分:0)

嗯,简短的回答是,只需关闭你的if (holder == null) {语句两行,这样就可以了:

if (holder == null) {
                    holder = new ViewHolder ();
                    view = _activity.Activity.LayoutInflater.Inflate (Resource.Layout.SegmentedControl, parent, false);
}

这是容易的部分(我希望),你遇到问题的原因是因为ListView中的视图被循环使用(以便保留旧值)但是它们不会被循环使用,这就是为什么你看到它表现不一致的原因。我建议你阅读ListView视图回收。这是an article我觉得有帮助。