Android should be an example for using ranking in a list
I am learning Android and want a code example or an example of using ratings to show ratings and not include onclick events on a list.
I am fetching data from mysql database using json. the same data after conversion to float should be displayed in the rating line in the list. I tried many examples here on stackoverflow but most of them seem incomplete.
thank
EDIT:
Below is the code that m uses to display string values in a 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 file:
<?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>
+3
source to share
2 answers
try the following code
<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);
still if you run into a problem than you prefer next project
+3
source to share