首页 > 解决方案 > 评级栏在回收站视图中为 0

问题描述

在我的应用程序中,我有一个包含餐厅名称和地址的回收站视图,我只是尝试向它添加一个评分栏,我从 SQL 数据库中获取所有数据,并使用 volley 连接到它,一切正常我什至可以发送餐厅的评级,但是当我尝试获取数据时,我得到了 0,并且我检查了 REST API 和 Json,当我在我的 Recycler 适配器类中使用没有数据库的最终值设置评级时它可以工作但是当我尝试在我的 MainActivity 中设置数据并将其放入我的 bindview 持有人它得到 0 请帮助

主要活动

public void sendRequest(){

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, request_url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                restaurants.clear();
                for (int i = 0; i < response.length(); i++) {
                    Restaurant restaurant = new Restaurant();

                    try {
                        JSONObject jsonObject = response.getJSONObject(i);
                        restaurant.setId(jsonObject.getInt("restaurant_id"));
                        restaurant.setName(jsonObject.getString("restaurant_name"));
                        restaurant.setAddress(jsonObject.getString("restaurant_address"));
                        restaurant.setImage(jsonObject.getInt("restaurant_image_type"));
                        restaurant.setHasNote(jsonObject.getBoolean("restaurant_has_note"));
                        restaurant.setRating((float) jsonObject.getInt("restaurant_rating"));
                        swipeRefreshLayout.setRefreshing(false);
                    } catch (JSONException e) {
                        swipeRefreshLayout.setRefreshing(false);
                        Log.i("VolleyGetError",e.toString());
                        e.printStackTrace();
                    }
                    restaurants.add(restaurant);
                    adapter.notifyDataSetChanged();
                    shimmerContainer.stopShimmerAnimation();
                    shimmerContainer.setVisibility(View.GONE);
                }
                fastfoodRecyclerView.setAdapter(adapter);
                swipeRefreshLayout.setRefreshing(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("Volley Error:", String.valueOf(error));
            }
        });
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        queue.add(jsonArrayRequest);

    }

我的 POJO 课

public Restaurant() {
    }

    public Restaurant(String name, String address,int type){
        this.name = name;
        this.address = address;
        this.type = type;
    }

    public Restaurant(int id, String name, String address,int type){
        this.id = id;
        this.name = name;
        this.address = address;
        this.type = type;
    }

    public Restaurant(int id,  String name, String address, int type,int image,int count) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.type = type;
        this.image = image;
        this.count = count;
    }

    public Restaurant(int id,  String name, String address, int type,int image,int count,float rating) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.type = type;
        this.image = image;
        this.count = count;
        this.rating = rating;
    }

    public float getRating() {
        return rating;
    }

    public int getCount() {
        return count;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public void setCount (int count) {this.count = count;}

    public int getId() { return id;}

    public void  setId (int id) { this.id = id;}

    public String getName() {
        return name;
    }

    public void setName(String name) { this.name = name;}

    public boolean isHasNote(){
        return hasNote;
    }

    public void setHasNote(boolean hasNote){
        this.hasNote = hasNote;
    }

    public String getAddress() { return address;}

    public void setAddress(String address) { this.address = address;}

    public void setType(int type) {this.type = type;}

    public int getType() {return type;}

    public int getImage() {return image;}

    public void setImage(int image) {this.image = image;}
}

我的回收器适配器类

        TextView listviewName,listviewAddress;
        ImageView icon,noteIcon,deleteicon;
        RelativeLayout viewBackground, viewForeground;
        RatingBar ratingBar;

        MyViewHolder(View view){
            super(view);
            ratingBar = view.findViewById(R.id.rating_bar);
            listviewName = view.findViewById(R.id.listview_name);
            listviewAddress = view.findViewById(R.id.listview_address);
            icon = view.findViewById(R.id.type_ic);
            noteIcon = view.findViewById(R.id.note_icon);
            deleteicon = view.findViewById(R.id.delete_icon);
            deleteicon.setVisibility(View.GONE);
            viewBackground = view.findViewById(R.id.view_background);
            viewForeground = view.findViewById(R.id.view_foreground);
            view.setOnCreateContextMenuListener(this);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onRestaurantSelected(restaurantListFiltered.get(getAdapterPosition()));
                }
            });
        }
        @Override
        public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            menu.add(0, 1, 0, "Edit");
            menu.add(0, 2, 1, "Remove");
            menu.add(0, 3, 2, "Add Note");
            menu.add(0, 4, 3, "All Notes");
        }
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.fadein);
        itemView.startAnimation(animation);
        return new MyViewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                setPosition(holder.getAdapterPosition());
                return false;
            }
        });
        Restaurant restaurant = restaurantList.get(position);
        holder.listviewName.setText(restaurant.getName());
        holder.listviewAddress.setText(restaurant.getAddress());
        holder.ratingBar.setRating(restaurant.getRating());
        switch (restaurant.getImage()) {
            case RestaurantContract.EntryRestaurants.RESTAURANT_TYPE_DELIVERY:
                holder.icon.setImageResource(R.drawable.phoneorderr);
                break;
            case RestaurantContract.EntryRestaurants.RESTAURANT_TYPE_SITDOWN:
                holder.icon.setImageResource(R.drawable.sitdownn);
                break;
            case RestaurantContract.EntryRestaurants.RESTAURANT_TYPE_TAKEAWAY:
                holder.icon.setImageResource(R.drawable.takeaway);
                break;
        }
        holder.noteIcon.setImageResource(R.drawable.notepadicon);
        if(restaurant.isHasNote()) {
            holder.noteIcon.setVisibility(View.VISIBLE);
        }else {
            holder.noteIcon.setVisibility(View.INVISIBLE);
        }

    }

我的 json

    {
        "restaurant_id": "97",
        "restaurant_name": "tt",
        "restaurant_address": "rr",
        "restaurant_type": "0",
        "restaurant_has_note": "0",
        "restaurant_image_type": "2",
        "restaurant_rating": "3"
    }
]

标签: androidjsonandroid-recyclerviewandroid-volley

解决方案


首先,您的 POJO 类为Restaurant. 将以下值添加到您的rating字段

public Restaurant(){
public float rating = 0.0f;
}

其次,您需要在MyViewHolder下面聆听您的评分栏更改:

  ratingBar = (RatingBar) view.findViewById(R.id.rating_bar);
        ratingBar .setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            restaurants.get(getPosition()).rating = ratingBar.getRating();
        }
    });

我看到你已经实现了一个arraylist名为 的restaurantList,我仍然提到了下面的实现以供参考:

然后在您的适配器中,您必须实现一个arraylist来为您的数据获取数据ratingBar

static List<Restaurant> restaurants= new ArrayList<>(); //restaurantList in your 

在您的构造函数中启动它。

this.restaurants = restaurants;//restaurantList in your case

然后在你的onBindViewHolder

List<String> restro= new ArrayList<String>();

     for(int i=0;i<restaurants.size();i++){
        restro.add(String.valueOf(restaurants.rating));//restaurantList in your case
    }

希望有帮助。


推荐阅读