【发布时间】: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