我有一个异步任务,通过检查数据库来检查用户是否有某个项目。如果他们有一个项目,那么我会给评级栏充气,如果没有,我会给按钮充气,这样他们就可以添加项目。
我在一个扩展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
}
});
}
findviewbyid在eclipse中给出了这个错误:
The method findViewById(int) is undefined for the type CheckBeerJSON
我认为是因为它没有扩展活动,所以我对如何准确实现它感到困惑。
inflated 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>
答案 0 :(得分: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
}
});
}