how to set rating bar value using simple adapter on android studio

225 Views Asked by At

This is my activity java class and use Volly libry to get data from server as a json array. so I want to get rating value from server and set it to the rating bar.

ListActivity.java

public void LoadList() {
    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://beezzserver.com/slthadi/projectPHI/place/index.php?Lid="+lid+"&gid="+gid+"&cid="+cid+"";

    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    setPlace(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });

    queue.add(request);
}

public void  setPlace(JSONArray response){

    List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

    try {
        for(int i=0; i<response.length(); i++){

            JSONObject object = response.getJSONObject(i);

            HashMap<String, String> map = new HashMap<>();
            map.put("id", object.getString("id"));
            map.put("name", object.getString("name"));
            map.put("rating", object.getString("reating"));  //need to set this value to rating bar

            map.put("img", Integer.toString(array[i]));
            list.add(map);

        }

        int layout = R.layout.item_placelist;
        int[] views = {R.id.pid,R.id.tvName,R.id.rtbPlace};        //this not work
        String[] colums = {"id","name","rating"};

        SimpleAdapter adapter = new SimpleAdapter(this,list,layout,colums,views);
        lv.setAdapter(adapter);

    }catch (Exception e){
        e.printStackTrace();
    }
}

This is my list item layout

item_placelist.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="110dp"
    android:layout_marginHorizontal="10dp">

   .............. other codings.....................

            <RatingBar
                android:id="@+id/rtbPlace"
                style="@style/Widget.AppCompat.RatingBar.Small"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:progressBackgroundTint="@color/textDark"
                android:progressTint="@color/colorGreenSpc"
                android:numStars="5"
                android:rating="3.5"
                android:layout_gravity="end"/>

            ......................other code...............

</LinearLayout>

How I set the value of rating bar using simple adapter? I trying to resole this using based some question in this. but I cant find some real solutio for thif.

0

There are 0 best solutions below