java - 更新安卓速率栏

标签 java android android-xml

我有一个在布局中膨胀的 android 费率栏。当我运行代码时,我从 json 获得了我想要更新 rateBar 的评级,我检查了我的日志以查看我确实获得了评级,我确实这样做了。但是当我尝试时:

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

它不会更新 Activity 的 rateBar。

我的 Activity 是啤酒页面:

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

}

最佳答案

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

ratingBar.setNumStars(beerRate);

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

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

r.setRating(beerRate);

关于java - 更新安卓速率栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17281396/

相关文章:

android - 在多个 View 上同步动画

android - 创建一个包含另一个可绘制对象和边距的可绘制对象

c# - 通过局域网将 Android 应用程序连接到 PC 应用程序

android - XMLPullParser getName() 返回 null

android - 元素类型 "item"必须由匹配的结束标记 "</item>"终止

java - 如何使这个正则表达式为负?

java - 在 imageView 上的 editText 中输入图像 URL

java - HIbernate Envers @AuditMappedBy 导致 SQLException : Parameter index out of range

java - 注销重定向 Spring Security

java - Android 类的 Mockito ClassNotFoundException