سؤال

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

            }
        });
      }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top