无法访问Android中膨胀的RatingBar [英] trouble accessing inflated ratingBar in Android
问题描述
我有一个异步的任务,检查如果用户有一定的项目,通过检查数据库。如果他们有一个项目,然后我一个充气棒评级,如果不是我吹一个按钮,这样他们可以添加的项目。
我夸大其与扩展的AsyncTask一个类的等级吧:
//膨胀星级评定者
LayoutInflater mInflater =(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
的LinearLayout Add按钮=(的LinearLayout)((活动)C).findViewById(R.id.addBeerLayout);
addButton.addView(mInflater.inflate(R.layout.addrate_layout,NULL)); addListenerOnRatingBar();
增加,这将调用另一个异步任务的评级保存到外部数据库侦听器时出了问题。
我addListenerOnRatingBar()方法是这样的:
公共无效addListenerOnRatingBar(){ 的RatingBar的RatingBar =(的RatingBar)findViewById(R.id.beerRatingBar);
//如果等级值被改变,
//自动显示在结果(TextView的)额定电流值
ratingBar.setOnRatingBarChangeListener(新OnRatingBarChangeListener(){
公共无效onRatingChanged(的RatingBar的RatingBar,浮动额定值,
布尔FROMUSER){ //接下来的异步任务来更新在线数据库 }
});
}
该findviewbyid给出了这样的错误在日食:
的方法findViewById(INT)是未定义的类型CheckBeerJSON
我以为是因为它没有延伸活动,所以我很困惑就如何准确地实现这一点。
充气ratebar xml文件:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的RatingBar
机器人:ID =@ + ID / beerRatingBar
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:numStars =5
机器人:stepSize的=1.0
机器人:等级=0/>
< / LinearLayout中>
我不知道,但假设你已经在你的 addrate_layout
布局采取的RatingBar。
如果它的情况,那么你必须从膨胀的布局找到的RatingBar。
有关〔实施例:
mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看查看= mInflater.inflate(R.layout.addrate_layout,NULL);
addListenerOnRatingBar(视图);
通过传递查看
修饰方法:
公共无效addListenerOnRatingBar(查看视图){ 的RatingBar的RatingBar =(的RatingBar)view.findViewById(R.id.beerRatingBar);
//如果等级值被改变,
//自动显示在结果(TextView的)额定电流值
ratingBar.setOnRatingBarChangeListener(新OnRatingBarChangeListener(){
公共无效onRatingChanged(的RatingBar的RatingBar,浮动额定值,
布尔FROMUSER){ //接下来的异步任务来更新在线数据库 }
});
}
I have an async task that check if a user has a certain item, by checking a database. If they have an item then I inflate a rating bar, if not I inflate a button so they can add the item.
I inflate the rating bar in an class which extends asyncTask with:
//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();
The problem is when adding the listener which will call another async task to save the rating to an external database.
My addListenerOnRatingBar() method looks like:
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
}
});
}
The findviewbyid gives this error in eclipse:
The method findViewById(int) is undefined for the type CheckBeerJSON
which I assume is because it does not extend activity, so I am confused on how to exactly implement this.
Inflated ratebar xml file:
<?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>
I am not sure but assuming you have taken RatingBar in your addrate_layout
layout.
If its the case then you have to find RatingBar from the inflated layout.
For exmaple:
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = mInflater.inflate(R.layout.addrate_layout, null);
addListenerOnRatingBar(view);
Modified method by passing 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
}
});
}
这篇关于无法访问Android中膨胀的RatingBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!