无法在Android的Simple Adapter中设置评分条

3
在我的Android项目中,有一个列表来展示从Google地点API返回的一些列表。在这里,Google API返回评分作为字符串值,我想将其添加到Simple Adapter中,在列表中显示评分条,请帮助我解决这个问题...
  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);

                }
            });

            }

    }
    }

1
我认为你应该自己回答并自己接受它。 - Niko
1个回答

0

RatingBar小部件应该放置在适配器中使用的列表行布局R.layout.listview_item_row中。目前,我只是将RatingBar小部件放置在主布局下面的ListView中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接