
我正在学习android,想要一个示例代码或使用ratingbar的示例仅用于显示评级,并且列表视图中没有onclick事件.
我正在使用Json从MysqL数据库中获取数据.转换为浮动格式后,相同的数据必须显示在列表中的评级栏中.我在这里尝试了许多关于stackoverflow的示例,但其中大多数似乎都不完整.
谢谢
编辑:
以下是代码m用于在列表中显示字符串值的代码
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" 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" 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" 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" 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" 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>解决方法:
您是否尝试过本教程?
我希望这个能帮上忙:
Android Tutorial 7.4 ListView with RatingBar
总结以上是内存溢出为你收集整理的android在listview中使用Ratingbar所需的示例全部内容,希望文章能够帮你解决android在listview中使用Ratingbar所需的示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)