onratingbarchanged和onratingbarclicked android之间的区别

时间:2014-08-13 09:43:44

标签: android onclicklistener ratingbar

我的Android应用程序中有一个评级栏,它位于自定义适配器中。我已将评级栏设置为侦听更改并根据该更改,通过客户端/服务器体系结构更新数据库。然后,我在主/详细信息视图中使用自定义适配器。问题是,每次我点击左侧列表项加载详细信息页面时,它都会更新评级栏。这不是我想要的。我只想在点击后更新评级栏,而不是每次使用适配器时都更新。

是否可以仅在单击并且未更改时触发事件。 onratingbarchanged(目前是哪个)和onratingbarclicked(我假设我应该做什么?)之间存在重大差异。

我的代码如下:

    //Should this rather be setOnClickListener()???
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
    {
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
        {
            questions.get(position).TotalRating = rating;
            String newRating = "" + rating;
            ratingBar.setRating(rating);
            Toast.makeText(getContext(),
                    "Rating set to:  " + rating + " for the position: " + position, Toast.LENGTH_SHORT).show();

            String question = questions.get(position).Question;

            //Create XML with both position/question to send to doAsyncTask
            serverUpdateRating update = new serverUpdateRating();
            Document doc;
            try 
            {
                //Create an XML document with question from the selected position as well as the new rating

                doc = x.createDoc();
                Element tutor = doc.createElement("Update");

                tutor.appendChild(x.UpdateRating(doc, newRating, question));

                doc.appendChild(tutor);

                //Create a string
                String s = x.getStringFromDocument(doc);

                String result = update.execute(s).get();
                //return either true (updated correctly) or false (problem)

                if (result.equals("true"))
                {
                    Toast.makeText(getContext(),
                            "Rating successfully updated", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(getContext(),
                            "Rating update unsuccessful", Toast.LENGTH_LONG).show();
                }

            }
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

我不知道是否有解决方法,但如果有,我将非常感激!

1 个答案:

答案 0 :(得分:-1)

您可以在fromUser

中使用onRatingChanged

onRatingChanged

  

[...] fromUser如果用户的触摸启动了评级更改,则为True   手势或箭头键/水平trackbell运动。