【问题标题】:trouble accessing inflated ratingBar in Android在 Android 中访问夸大的 ratingBar 时遇到问题
【发布时间】:2013-06-24 03:55:48
【问题描述】:

我有一个异步任务,通过检查数据库来检查用户是否有某个项目。如果他们有项目,我会为评分栏充气,如果没有,我会为按钮充气,以便他们添加项目。

我在一个扩展 asyncTask 的类中夸大了评分栏:

//inflate star rater
                LayoutInflater mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout addButton = (LinearLayout)((Activity) c).findViewById(R.id.addBeerLayout);
                addButton.addView(mInflater.inflate(R.layout.addrate_layout, null));

                addListenerOnRatingBar();

问题在于添加监听器时,它将调用另一个异步任务将评级保存到外部数据库。

我的 addListenerOnRatingBar() 方法如下所示:

public void addListenerOnRatingBar() {

        RatingBar ratingBar = (RatingBar) findViewById(R.id.beerRatingBar);


        //if rating value is changed,
        //display the current rating value in the result (textview) automatically
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {

                //next async task to update online database

            }
        });
      }

在eclipse中findviewbyid给出了这个错误:

The method findViewById(int) is undefined for the type CheckBeerJSON

我认为是因为它不扩展活动,所以我对如何准确实现这一点感到困惑。

膨胀的ratebar xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <RatingBar
        android:id="@+id/beerRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1.0"
        android:rating="0" />


</LinearLayout>

【问题讨论】:

  • 您是否从膨胀视图中寻找评分栏? inflatedviewlayout.findViewById(int)
  • 不确定你的意思。 addlistener 函数在我的异步任务中

标签: android android-xml android-inflate ratingbar


【解决方案1】:

我不确定,但假设您在 addrate_layout 布局中使用了 RatingBar。

如果是这种情况,那么您必须从膨胀的布局中找到 RatingBar。

例如:

mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.addrate_layout, null);
addListenerOnRatingBar(view);

通过View修改方法:

public void addListenerOnRatingBar(View view) {

        RatingBar ratingBar = (RatingBar) view.findViewById(R.id.beerRatingBar);


        //if rating value is changed,
        //display the current rating value in the result (textview) automatically
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {

                //next async task to update online database

            }
        });
      }

【讨论】:

  • inflate in mInflater.inflate 等给出此错误:Inflater 类型中的方法 inflate(byte[]) 不适用于参数 (int, null)
  • 希望您已正确初始化 mInflater。检查更新的代码。
  • 太棒了,但现在我在侦听器部分收到此错误:RatingBar 类型中的方法 setOnRatingBarChangeListener(RatingBar.OnRatingBarChangeListener) 不适用于参数(新 OnRatingBarChangeListener(){})
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 2016-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多