0

JSON から取得した評価値を評価バーに更新しようとしています


値をtextvieに更新することはできましたが...........JSON評価から評価バーに値を更新する方法


これらは私が使用したクラスです

MainActivity.java

public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String NAME = "rank";
    static String TYPE = "country";
    static String DISTANCE = "distance";
    static String RATING = "rating";
    static String FLAG = "flag";
    static String PRICE= "price";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.listview_main);


        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);

        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            //mProgressDialog.setTitle("Fetching the information");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions.getJSONfromURL("url");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("restaurants");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put(MainActivity.NAME, jsonobject.getString("restaurantNAME"));
                    map.put(MainActivity.TYPE, jsonobject.getString("restaurantTYPE"));
                    map.put(MainActivity.FLAG, "http://54.218.73.244:7005/"+jsonobject.getString("restaurantIMAGE"));
                    map.put(MainActivity.DISTANCE, jsonobject.getString("restaurantDISTANCE"));
                    map.put(MainActivity.RATING, jsonobject.getString("restaurantRATING"));
                    map.put(MainActivity.PRICE, jsonobject.getString("restaurantPrice"));

                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
        imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView name;
        TextView type;
        TextView distance;
        TextView rating;
        Button price;
        ImageView flag;
        RatingBar ratingbar;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        name = (TextView) itemView.findViewById(R.id.RestaurantNameID);
        type = (TextView) itemView.findViewById(R.id.RestaurantTypeID);
        distance = (TextView) itemView.findViewById(R.id.RestaurantDistanceID);
        rating = (TextView) itemView.findViewById(R.id.RestaurantRatingID);
        price=(Button) itemView.findViewById(R.id.BuyButton);


        ratingbar=(RatingBar) itemView.findViewById(R.id.ratingIndicator);

        // Locate the ImageView in listview_item.xml
        flag = (ImageView) itemView.findViewById(R.id.flag);

        // Capture position and set results to the TextViews
        name.setText(resultp.get(MainActivity.NAME));
        type.setText(resultp.get(MainActivity.TYPE));
        distance.setText(resultp.get(MainActivity.DISTANCE));
        rating.setText(resultp.get(MainActivity.RATING));
        price.setText(resultp.get(MainActivity.PRICE));

        //ratingbar.setRating(resultp.get(MainActivity.RATING));

        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
        imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, RestaurantDesc.class);
                // Pass all data rank
                intent.putExtra("REST", resultp.get(MainActivity.NAME));
                // Pass all data country
                   //intent.putExtra("type", resultp.get(MainActivity.TYPE));
                // Pass all data flag
                   //intent.putExtra("flag", resultp.get(MainActivity.FLAG));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });
        return itemView;
    }
}

listview_item.xml

<RatingBar
        android:id="@+id/ratingIndicator"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/RestaurantPicImageLinearViewID"
        android:layout_centerHorizontal="true" />

何か案は !

4

1 に答える 1

1

あなたのonPostExecute()

RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingIndicator);
ratingBar.setEnabled(false);
ratingBar.setMax(5); // I assume 5 is your max rating
ratingBar.setRating(Integer.parseInt(arraylist.get(MainActivity.RATING)));
于 2013-10-06T14:20:11.510 に答える