2013-06-24 85 views
1

我有一個android酒吧,充氣到佈局。當我運行代碼時,我得到了我想要從json更新rateBar的評級,然後查看我的日誌,看看我是否真的獲得了評分。但是當我嘗試時:更新android酒吧酒吧

RatingBar ratingBar = (RatingBar) ((Activity) c).findViewById(R.id.beerRatingBar); 
ratingBar.setNumStars(beerRate); 

它不會更新活動的rateBar。

我的活動是啤酒頁:

public class BeerPage extends Activity { 

    BeerData e; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.beer_page); 

     //get data from listview 
     Intent intent = getIntent(); 
     Bundle b = intent.getExtras(); 
     e = b.getParcelable("myBeerObject"); 

     //prepare buttons 
     Button buttonBrewery = (Button) findViewById(R.id.buttonBrewery); 
     Button buttonStyle = (Button) findViewById(R.id.buttonStyle); 

     //prepare text things 
     TextView tv1 = (TextView) findViewById(R.id.beerTitle); 
     TextView tv2 = (TextView) findViewById(R.id.beerDescription); 
     TextView tv_ibu = (TextView) findViewById(R.id.IBU); 
     TextView tv_abv = (TextView) findViewById(R.id.abv); 
     TextView tv_glass = (TextView) findViewById(R.id.glass); 

     //set text thinsg 
     tv1.setText(e.beerName); 
     tv2.setText(e.beerDescription); 
     buttonBrewery.setText(e.beerBreweryName); 
     buttonStyle.setText(e.beerStyle); 
     tv_ibu.setText(e.beerIBU); 
     tv_abv.setText(e.beerABV); 
     tv_glass.setText(e.beerGlass); 

     //Toast.makeText(this, e.mediumLabel, Toast.LENGTH_SHORT).show(); 

     //set image 
     ImageView im1 = (ImageView) findViewById(R.id.image); 
     ImageDownloadTask imageD = new ImageDownloadTask(im1); 
     imageD.execute(e.largeLabel); 

     //test shared prefs 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     String userName = prefs.getString("userName", null); 
     String userID = prefs.getString("userID", null); 

     //check if user has beer 
     String url = myURL; 
     String userURLComp = user; 
     String beerID = beerID; 

     url = url + userURLComp + beerID; 

     Log.d("lat", e.beerBreweryLat); 
     Log.d("long", e.beerBreweryLong); 

     new CheckBeerJSON(this,e).execute(url); 




    } 

    //view brewery function 
    public void viewBrewery(View view) { 

     // launch new brewery page class 
     Intent i = new Intent(this, BreweryPage.class); 
     i.putExtra("myBeerObject", e); 
     i.setClass(this, BreweryPage.class); 


     startActivity(i); 


    } 

public void viewStyle(View view) { 

     // launch new brewery page class 
     Intent i = new Intent(this, BreweryPage.class); 
     i.putExtra("myBeerObject", e); 
     i.setClass(this, StylePage.class); 


     startActivity(i); 


    } 

public String encodeThisWord(String word){ 

    try { 
     word = URLEncoder.encode(word, "UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

     return word; 

} 

public void addBeer(View view){ 

    //get user info 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
    String userName = prefs.getString("userName", null); 
    String userID = prefs.getString("userID", null); 


    //get beer details 
    String url = url2; 
    String urlUserID = userID; 
    String urlBeerID = beer + e.beerId; 
    String urlBeerName = beerName + encodeThisWord(e.beerName); 




    //construct url for adding beer 
    url = url + urlUserID + urlBeerID + urlBeerName; 

    Log.d("url", url); 

    //execute async on url to add to brewery 
    new AddBeer(this).execute(url); 

    //to do: change to start rater 

} 





} 

GetUserRating是從哪裏獲得的評級放入rateBar:

public class GetUserRating extends AsyncTask 
<String, Void, String> { 

    Context c; 

    public GetUserRating(Context context) 
    { 
     c = context; 
    } 

    @Override 
    protected String doInBackground(String... arg0) { 
     // TODO Auto-generated method stub 
     return readJSONFeed(arg0[0]); 
    } 

protected void onPostExecute(String result){ 

     int beerRate = 0; 

     //parse json for value 
     try{ 

      JSONObject json = new JSONObject(result); 
      beerRate = json.getInt("rate"); 




     } 
     catch(Exception e){ 

     } 


     Log.d("logIN", "the rating: " + beerRate); 

     //change rating 
     RatingBar ratingBar = (RatingBar) ((Activity) c).findViewById(R.id.beerRatingBar); 
     ratingBar.setNumStars(beerRate); 





    } 

    public String readJSONFeed(String URL) { 
     StringBuilder stringBuilder = new StringBuilder(); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(URL); 
     try { 
      HttpResponse response = httpClient.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { 
       HttpEntity entity = response.getEntity(); 
       InputStream inputStream = entity.getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(inputStream)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        stringBuilder.append(line); 
       } 
       inputStream.close(); 
      } else { 
       Log.d("JSON", "Failed to download file"); 
      } 
     } catch (Exception e) { 
      Log.d("readJSONFeed", e.getLocalizedMessage()); 
     }   
     return stringBuilder.toString(); 
    } 

} 

回答

1

我發現我的問題,我改變這一出數星星:這是在事實上改變顯示數星星

ratingBar.setNumStars(beerRate); 

。我想突出顯示所顯示的5顆星中的一定數量,而不是改變顯示的星星數量。

我真正想要做的是設置恆星:

r.setRating(beerRate); 
0

我認爲更新視圖項目在asyncTasck直接是餿主意。 我建議在BeerPage中創建一個公共靜態成員來更新啤酒評級。

問候