1

我有一个 android rate bar,它被膨胀成一个布局。当我运行代码时,我得到了我想从 json 更新 rateBar 的评级,然后我检查我的日志以查看我实际上得到了一个评级,我这样做了。但是当我尝试时:

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();
    }

}
4

2 回答 2

1

我发现了我的问题,我正在更改显示的星数:

ratingBar.setNumStars(beerRate);

这实际上改变了显示的星星数量。我想突出显示一定数量的 5 颗星,而不是更改显示的星数。

我真正想做的是设置星星:

r.setRating(beerRate);
于 2013-06-24T23:36:15.787 回答
0

我认为直接更新 asyncTasck 中的视图项是个坏主意。我建议在 BeerPage 中创建一个公共静态成员来更新啤酒评级。

问候

于 2013-06-24T18:15:41.813 回答