繁体   English   中英

无法在android中的Simple Adapter中设置评级栏

[英]Can't set the rating bar in Simple Adapter in android

在我的Android项目中有一个列表,显示来自Google Places API的一些列表,Google API将评级作为字符串值返回我想将其添加到简单适配器以显示列表中的评级栏请帮我修复此问题问题....

  import java.util.ArrayList;
    import java.util.HashMap;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;



    public class MyListActivity extends ListActivity {

        // JSON Node names
                    private static final String TAG_CONTACTS = "results";
                    private static final String TAG_ID = "id";
                    private static final String TAG_NAME = "name";
                    private static final String TAG_GENDER = "rating";
    //              private static final String TAG_ADDRESS = "formatted_address";
                    private static final String TAG_REFERENCE = "reference";
                    private static final String TAG_ICON = "icon";

        // contacts JSONArray
        JSONArray result = null;

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

            // url to make request

            Intent in = getIntent();
             String location = in.getStringExtra("TAG_LOCATION");
             String type = in.getStringExtra("TAG_TYPE");
            url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+type+"+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA";
            //}

    //      LoadData ld = new LoadData();
    //      ld.onPreExecute();
        new LoadData().execute();

        }

    class LoadData extends AsyncTask<String, String, String>
        { 

    //  ProgressDialog pDialog;
    //  protected void onPreExecute() {
    //      
    //pDialog = new ProgressDialog(MyListActivity.this);    
    //pDialog.setMessage("Populating list please wait...");
    //      pDialog.setIndeterminate(false);
    //      pDialog.setCancelable(false);
    //      pDialog.show();
    //}

        // Hashmap for ListView
            ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
        protected String doInBackground(String... args) {


            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();
            // getting JSON string from URL

            JSONObject json = jParser.getJSONFromUrl(url);

            try {
                // Getting Array of Contacts
                result = json.getJSONArray(TAG_CONTACTS);
                // looping through All Contacts
                for(int i = 0; i < result.length(); i++){
                    JSONObject c = result.getJSONObject(i);



                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
    //              String email = c.getString(TAG_EMAIL);
    //              String icon = c.getString(TAG_ICON);
                    String rating = c.getString(TAG_GENDER);

                    // Phone number is agin JSON Object
    //              JSONObject phone = c.getJSONObject(TAG_PHONE);
    //              String mobile = phone.getString(TAG_PHONE_MOBILE);
    //              String home = phone.getString(TAG_PHONE_HOME);
    //              String office = phone.getString(TAG_PHONE_OFFICE);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
    //              map.put(TAG_ICON, icon);
                    map.put(TAG_GENDER, rating);
    //              map.put(TAG_EMAIL, email);
    //              map.put(TAG_PHONE_MOBILE, mobile);

                    // adding HashList to ArrayList
                    contactList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
                }    
            protected void onPostExecute(String file_url) {

    //      this.pDialog.cancel();
                    // dismiss the dialog after getting all products
                        /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(MyListActivity.this, contactList,
                    R.layout.listview_item_row,
                    new String[] { TAG_NAME, TAG_GENDER,}, new int[] {
                            R.id.txtTitle, R.id.ratingbar });

            setListAdapter(adapter);

            // selecting single ListView item
            ListView lv = getListView();

            // Launching new screen on Selecting Single ListItem
            lv.setOnItemClickListener(new OnItemClickListener() {


                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // getting values from selected ListItem
    //              String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
    //              String cost = ((TextView) view.findViewById(R.id.formatted_address)).getText().toString();
    //              String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();

                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class);
    //              in.putExtra(TAG_NAME, name);
    //              in.putExtra(TAG_EMAIL, cost);
    //              in.putExtra(TAG_PHONE_MOBILE, description);
                    startActivity(in);

                }
            });

            }

    }
    }

RatingBar小部件应放在适配器中使用的列表行布局中,即R.layout.listview_item_row 现在,我只是在主布局中的ListView下面放置了一个RatingBar小部件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM