ndroid在listview中使用Ratingbar所需的示例

ndroid在listview中使用Ratingbar所需的示例

本文介绍了android在listview中使用Ratingbar所需的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在学习android,想要一个示例代码或使用Ratingbar的示例仅用于显示评级,并且列表视图中没有onclick事件.

I am learning android and want a sample code or an example for using ratingbar just to display ratings and no onclick events within a listview.

我正在使用json从mysql数据库中获取数据.转换为浮点数后,相同的数据必须显示在列表内的评级栏中.我在这里尝试了许多关于stackoverflow的示例,但其中大多数似乎都不完整.

I am fetching the data from a mysql database using json. the same data after being converted into float must be displayed in ratingbar within the list. I tried out many examples here on stackoverflow but most of them seem incomplete.

谢谢

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

Following is the code which m using to display string values in the list

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>

推荐答案

您尝试过本教程吗?

我希望这会有所帮助:

带有RatingBar的Android教程7.4 ListView

这篇关于android在listview中使用Ratingbar所需的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:15