2013-06-24 144 views
0

訪問充氣的RatingBar我有一個異步的任務,檢查用戶是否有某一個項目,通過檢查數據庫。如果他們有一個項目,然後我膨脹一個評級欄,如果沒有我膨脹一個按鈕,以便他們可以添加該項目。麻煩的Android

我誇大評級酒吧與擴展的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給出了日食此錯誤:

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> 
+0

難道你看從膨脹的觀點的評價吧? 'inflatedviewlayout.findViewById(INT)' – kabuto178

+0

不知道你的意思。 addlistener函數在我的異步任務 – Mike

回答

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 

      } 
     }); 
     } 
+0

infIn在mInflater.inflate等給出此錯誤: Inflater類型的inflate(byte [])方法不適用於參數(int,null) – Mike

+0

希望你有正確的初始化mInflater。檢查udpated代碼。 –

+0

真棒,但現在我得到這個錯誤與聽者的部分:在類型的RatingBar的方法setOnRatingBarChangeListener(RatingBar.OnRatingBarChangeListener)不適用於參數(新OnRatingBarChangeListener(){}) – Mike