android在listview中使用ratingbar所需的示例

时间:2013-02-08 09:25:31

标签: android

我正在学习android并希望使用示例代码或使用ratingbar的示例来显示列表视图中的评级和没有onclick事件。

我使用json从mysql数据库中获取数据。转换为float后的相同数据必须显示在列表中的ratingbar中。我在这里尝试了很多关于stackoverflow的例子,但大部分都看似不完整。

谢谢

编辑:

以下是用于在列表中显示字符串值的代码

public class ReadReviewActivity extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;
    String pid;
    String ratings1;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;

    // url to get all products list
    private static String url_all_products = "http://10.0.2.2/hm_andro/get_reviews.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_PID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_COMMENTS = "comments";
    private static final String TAG_RATINGS = "ratings";
    private static final String TAG_PIDD = "pid";
    private static final String TAG_DATE = "createddate";


    // products JSONArray
    JSONArray products = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rev);

        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();

        Intent i = getIntent();
        pid = i.getStringExtra(TAG_PIDD);


        // Get listview
        ListView lv = getListView();

        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                // sending pid to next activity

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });

    }

    // Response from Edit Product Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ReadReviewActivity.this);
            pDialog.setMessage("Loading products. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("pidd", pid));
            // getting JSON string from URL
            //params.add(new BasicNameValuePair("pid", "745"));
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            // Check your log cat for JSON response
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);


                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);
                         String comments = c.getString(TAG_COMMENTS);
                         String cdate = c.getString(TAG_DATE);
                    String rati = c.getString(TAG_RATINGS);

                    String rate1 [] = rati.split(",");
                    ratings1 = rate1[0];


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);
                    map.put(TAG_COMMENTS, comments);
                    map.put(TAG_DATE, cdate);
                    map.put(TAG_RATINGS,ratings1);

                        // adding HashList to ArrayList
                        productsList.add(map);

                    }
                } else {

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

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ReadReviewActivity.this, productsList,
                            R.layout.review_list, new String[] {TAG_NAME, TAG_COMMENTS, TAG_DATE},
                            new int[] {R.id.revname, R.id.revcomments, R.id.reviewdate});
                    // updating listview

                    setListAdapter(adapter);
                }
            });

        }


    }
    }

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:visibility="visible" >

    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:text="Reviewed by:"
        android:textColor="@color/blueish"
        android:textSize="15sp"
        android:visibility="visible" />

    <TextView
        android:id="@+id/revname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textColor="@color/magenta"
        android:textSize="17sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Overall Ratings:   "
        android:paddingLeft="6dip"
        android:textColor="@color/blueish"
        android:textSize="11sp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:text="Service/Hospitality:   "
            android:paddingLeft="6dip"
            android:textColor="@color/blueish"
            android:textSize="11sp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView2"
            android:paddingLeft="6dip"
            android:text="Clinical Treatment:   "
            android:textColor="@color/blueish"
            android:textSize="11sp" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView3"
            android:paddingLeft="6dip"
            android:text="Infrastructure:   "
            android:textColor="@color/blueish"
            android:textSize="11sp" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingLeft="6dip"
            android:layout_below="@+id/textView4"
            android:text="Value for Money:   "
            android:textColor="@color/blueish"
            android:textSize="11sp" />

        <RatingBar
            android:id="@+id/Bar1"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView1"
            android:layout_toRightOf="@+id/textView1" />

        <RatingBar
            android:id="@+id/Bar2"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView2"
            android:layout_toRightOf="@+id/textView2" />

        <RatingBar
            android:id="@+id/Bar3"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView3"
            android:layout_toRightOf="@+id/textView3" />

        <RatingBar
            android:id="@+id/Bar4"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView4"
            android:layout_toRightOf="@+id/textView4" />

        <RatingBar
            android:id="@+id/Bar5"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView5"
            android:layout_toRightOf="@+id/textView5" />

    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:text="Comments:"
        android:textColor="@color/blueish"
        android:textSize="15sp"
        android:visibility="visible" />

    <TextView
        android:id="@+id/revcomments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textColor="@color/magenta"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/reviewon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Created On :"
        android:textColor="@color/blueish" />

    <TextView
        android:id="@+id/reviewdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/magenta" />

</LinearLayout>

2 个答案:

答案 0 :(得分:3)

尝试以下代码

<RatingBar 
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:isIndicator="true"
    android:numStars="5"
    android:stepSize="1"
    android:rating="5"
    android:id="@+id/pop_ratingbar"
    style="?android:attr/ratingBarStyleSmall"
/>

RatingBar ratingBar = (RatingBar) findviewbyid(R.id.pop_ratingbar);
ratingBar.setRating(4.0f);

如果您遇到问题,请选择以下project

答案 1 :(得分:1)

您是否尝试过本教程?

我希望这会有所帮助:

Android Tutorial 7.4 ListView with RatingBar