在OnResponse内部的Volley JsonObjectRequest Recyclerview适配器

时间:2015-11-16 02:14:20

标签: android android-studio android-volley jsonobjectrequest

我有一个数据库。(XAMMP)
我能够获得所有的价值 我正在使用回收站视图,并根据我的数据库中的值填充recyclerview 我把recyclerview适配器放在响应中,
但是textviews的静态值已经消失了。 有没有更好的方法来填充recyclerview(onBackground)?

public void getComments() {
        final List<Comments> commentsData = new ArrayList<>();
        Global.getCommentTitle.clear();
        Global.getCommentPicture.clear();
        Global.getComment.clear();
        Global.getCommentDate.clear();
        Global.getCommentRating.clear();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                showUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("poi");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Global.getCommentTitle.add(jsonObject.getString("title"));
                        Global.getCommentPicture.add("drawable://" + R.drawable.juandirection_placeholder);
                        Global.getComment.add(jsonObject.getString("comment"));
                        Global.getCommentDate.add(jsonObject.getString("date"));
                        Global.getCommentRating.add(Integer.parseInt(jsonObject.getString("rating")));
                        if (i < 5) {
                            Comments current = new Comments();
                            current.image = "drawable://" + R.drawable.juandirection_placeholder;
                            current.title = jsonObject.getString("title");
                            current.comment = jsonObject.getString("comment");
                            current.date = jsonObject.getString("date");
                            current.rating = Integer.parseInt(jsonObject.getString("rating"));
                            commentsData.add(current);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                runOnUiThread(new Runnable() {
                    public void run() {
                        List<Information> headerData = new ArrayList<>();
                        int[] icons = {R.drawable.ic_information, R.drawable.ic_place_black_36dp, R.drawable.ic_local_see_black_24dp};
                        for (int i = 0; i < Global.getHeader.size(); i++) {
                            Information headerInfo = new Information();
                            headerInfo.iconId = icons[i];
                            headerInfo.title = Global.getHeader.get(i);
                            headerData.add(headerInfo);
                        }

                        if (commentsData.size() == 5) {
                            Information current = new Information();
                            current.iconId = R.drawable.ic_comment_black_24dp;
                            current.title = "View more Comments";
                            headerData.add(current);
                        }
                        Log.e("SIZE", "" + commentsData.size());


                        List<Rating> ratingData = new ArrayList<>();
                        Rating ratingInfo = new Rating();
                        ratingInfo.reviewTxt = "Bulacan State University";
                        ratingData.add(ratingInfo);
                        ratingInfo.ratingBarReview = 2;
                        ratingData.add(ratingInfo);
                        ratingInfo.progressbarE = 20;
                        ratingData.add(ratingInfo);
                        ratingInfo.progressbarVG = 40;
                        ratingData.add(ratingInfo);
                        ratingInfo.progressbarAVG = 60;
                        ratingData.add(ratingInfo);
                        ratingInfo.progressbarP = 80;
                        ratingData.add(ratingInfo);
                        ratingInfo.progressbarT = 95;
                        ratingData.add(ratingInfo);


                        List<Header> headerData2 = new ArrayList<>();
                        String[] header = {"Information", "Photos", "Reviews"};
                        for (int i = 0; i < header.length; i++) {
                            Header current = new Header();
                            current.header = header[i];
                            headerData2.add(current);
                        }

                        mRecyclerView = (RecyclerView) findViewById(R.id.rvPOI);
                        adapter = new AdapterPOI(getApplicationContext(), headerData, headerData2, ratingData, commentsData);
                        mRecyclerView.setAdapter(adapter);
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                        mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
                        mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
                            @Override
                            public void onClick(View view, int position) {
                                if (position == 2) {
                                    startActivity(new Intent(getApplicationContext(), ViewInformation.class));
                                }
                                if (position == 3) {
                                    Toast.makeText(getApplicationContext(), "Map", Toast.LENGTH_SHORT).show();
                                }
                                if (position == 6) {
                                    Toast.makeText(getApplicationContext(), "Add Photos", Toast.LENGTH_SHORT).show();
                                }
                                if (position >= 9 && position <= 13) {
                                    Global.viewCommets = position - 9;
                                    Global.singleComment = true;
                                    startActivity(new Intent(getApplicationContext(), ViewComment.class));
                                }
                                if (position == 14) {
                                    Global.singleComment = false;
                                    startActivity(new Intent(getApplicationContext(), ViewComment.class));
                                }
                            }

                            @Override
                            public void onLongClick(View view, int position) {

                            }
                        }));
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
    }  

甚至Recyclerview的涟漪效应也已消失。 什么似乎是问题?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginBottom="1dp"
        android:background="#FFF"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/reviewTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:paddingLeft="16dp"
                android:text="Bulacan State University"
                android:textColor="@color/primaryColor"
                android:textSize="15dp" />


        </LinearLayout>


        <RatingBar
            android:id="@+id/ratingBarReview"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:numStars="5"
            android:progressDrawable="@drawable/custom_ratingbar"
            android:rating="3.5"
            android:stepSize=".5"
            />


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingLeft="16dp"
                android:paddingTop="8dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/Excelent"
                    android:textSize="13dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Very Good"
                    android:textSize="13dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Average"
                    android:textSize="13dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Poor"
                    android:textSize="13dp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Terrible"
                    android:textSize="13dp" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="13dp"
                android:orientation="vertical"
                android:paddingLeft="16dp"
                android:paddingRight="14dp">

                <ProgressBar
                    android:id="@+id/progressbarE"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="9dip"
                    android:layout_gravity="center_horizontal"
                    android:focusable="false"
                    android:maxHeight="10dip"
                    android:minHeight="10dip"
                    android:progress="90"
                    android:progressDrawable="@drawable/custom_progressbar" />

                <ProgressBar
                    android:id="@+id/progressbarVG"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="9dip"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="8dp"
                    android:focusable="false"
                    android:maxHeight="10dip"
                    android:minHeight="10dip"
                    android:progress="75"
                    android:progressDrawable="@drawable/custom_progressbar" />

                <ProgressBar
                    android:id="@+id/progressbarAVG"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="9dip"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="8dp"
                    android:focusable="false"
                    android:maxHeight="10dip"
                    android:minHeight="10dip"
                    android:progress="65"
                    android:progressDrawable="@drawable/custom_progressbar" />


                <ProgressBar
                    android:id="@+id/progressbarP"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="9dip"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="8dp"
                    android:focusable="false"
                    android:maxHeight="10dip"
                    android:minHeight="10dip"
                    android:progress="45"
                    android:progressDrawable="@drawable/custom_progressbar" />

                <ProgressBar
                    android:id="@+id/progressbarT"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="9dip"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="8dp"
                    android:focusable="false"
                    android:maxHeight="10dip"
                    android:minHeight="10dip"
                    android:progress="10"
                    android:progressDrawable="@drawable/custom_progressbar" />


            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

AdapterPOI ..

public class AdapterPOI extends RecyclerView.Adapter<AdapterPOI.MainViewHolder> {
    List<Information> data = Collections.emptyList();
    List<Header> headerData = Collections.emptyList();
    List<Rating> ratingsData = Collections.emptyList();
    List<Comments> commentsData = Collections.emptyList();
    private LayoutInflater inflater;
    private Context context;
    int viewType = 0;
    int headerCounter = 0;
    int vCounter = 0;
    int rCounter = 0;
    int cCounter = 0;
    int[] routeImg = {R.drawable.ic_resto_route, R.drawable.ic_sights_route, R.drawable.ic_transport_route, R.drawable.ic_hotel_route,
            R.drawable.ic_gas_route, R.drawable.ic_school_route, R.drawable.ic_entertainment_route,
            R.drawable.ic_shop_route, R.drawable.ic_atm_route, R.drawable.ic_bank_route, R.drawable.ic_hospital_route,
            R.drawable.ic_pharmacy_route, R.drawable.ic_police_route, R.drawable.ic_toilet_route,};

    public AdapterPOI(Context context, List<Information> data, List<Header> headerData, List<Rating> ratingsData, List<Comments> commentsData) {
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.headerData = headerData;
        this.context = context;
        this.ratingsData = ratingsData;
        this.commentsData = commentsData;
    }


    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int listViewItemType = getItemViewType(viewType);
        Log.e("FUCK ME",""+commentsData.size());
        if(listViewItemType == 0 ){
            return new TitleHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_title, parent, false));
        }
        if (listViewItemType == 1 || listViewItemType == 4 || listViewItemType == 7) {
            return new HeaderHolder(LayoutInflater.from(context).inflate(R.layout.headerinformation, parent, false));
        }if (listViewItemType == 2 || listViewItemType == 6 || listViewItemType == 3 || listViewItemType == 14) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_pictext, parent, false));
        }if (listViewItemType == 5) {
            return new GalleryHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_photos, parent, false));
        }if (listViewItemType == 8) {
            return new RatingHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_rating, parent, false));
        }if (listViewItemType >= 9 && listViewItemType<=8+commentsData.size()) {
            return new CommentsHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_comments, parent, false));
        } if (listViewItemType >= 9+commentsData.size()) {
            return new TapHolder(LayoutInflater.from(context).inflate(R.layout.custom_row_taptorate, parent, false));
        }

        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        Log.e("TAE: ",""+Global.selectedPOI);
        if(position==0){

            TitleHolder mHolder = (TitleHolder) holder;
            mHolder.imgType.setBackgroundResource(routeImg[Global.getPosition]);
            mHolder.txtTitle.setText(Global.selectedPOI);
            //imgFavorite=(ImageView)itemView.findViewById(R.id.imgFavorite);
            //txtTitle=(TextView)itemView.findViewById(R.id.txtTitle);
            //ratingBar=(RatingBar)itemView.findViewById(R.id.ratingBar);
        }

        if (position == 1 || position == 4 || position == 7) {
            if (position == 1) {
                headerCounter = 0;
            }
            if (position == 4) {
                headerCounter = 1;
            }
            if (position == 7) {
                headerCounter = 2;
            }
            Header current = headerData.get(headerCounter);
            HeaderHolder mHolder = (HeaderHolder) holder;
            mHolder.txtInfo.setText(current.header);
        }

        if (commentsData.size() == 5) {
            if (position == 14) {
                vCounter = 3;
                Information current = data.get(vCounter);
                MyViewHolder mHolder = (MyViewHolder) holder;
                mHolder.title.setText(current.title);
                mHolder.icon.setImageResource(current.iconId);
            }
        }

        if (position == 2 || position == 3 || position == 6) {
            if (position == 2) {
                vCounter = 0;
            }
            if (position == 3) {
                vCounter = 1;
            }
            if (position == 6) {
                vCounter = 2;
            }
            Information current = data.get(vCounter);
            MyViewHolder mHolder = (MyViewHolder) holder;
            mHolder.title.setText(current.title);
            mHolder.icon.setImageResource(current.iconId);

        }
        if (position == 5) {
            GalleryHolder mHolder = (GalleryHolder) holder;
            GalleryViewAdapterPOI galleryViewAdapter;
            galleryViewAdapter = new GalleryViewAdapterPOI(context, Global.IMAGES);
            mHolder.gallery.setAdapter(galleryViewAdapter);
            final WindowManager display = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

            DisplayMetrics metrics = new DisplayMetrics();
            display.getDefaultDisplay().getMetrics(metrics);
            ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mHolder.gallery.getLayoutParams();
            mlp.setMargins(-((metrics.widthPixels * 2 + 200) / 3), mlp.topMargin,
                    mlp.rightMargin, mlp.bottomMargin);
        }
        if (position == 8) {
            Rating current = ratingsData.get(0);
            RatingHolder mHolder = (RatingHolder) holder;
            mHolder.reviewTxt.setText(current.reviewTxt);
            mHolder.ratingBarReview.setRating(current.ratingBarReview);
            mHolder.progressbarE.setProgress(current.progressbarE);
            mHolder.progressbarVG.setProgress(current.progressbarVG);
            mHolder.progressbarAVG.setProgress(current.progressbarAVG);
            mHolder.progressbarP.setProgress(current.progressbarP);
            mHolder.progressbarT.setProgress(current.progressbarT);
        }

        if (position >= 9 && position <= 8 + commentsData.size()) {
            if (position == 9) {
                cCounter = 0;
            }
            if (position == 10) {
                cCounter = 1;
            }
            if (position == 11) {
                cCounter = 2;
            }
            if (position == 12) {
                cCounter = 3;
            }
            if (position == 13) {
                cCounter = 4;
            }
            Comments current = commentsData.get(cCounter);
            CommentsHolder mHolder = (CommentsHolder) holder;
            Picasso.with(context)
                    .load(current.image)
                    .placeholder(R.drawable.juandirection_placeholder)
                    .centerCrop()
                    .fit()
                    .into(mHolder.commentPhoto);
            mHolder.commentTitle.setText(current.title);
            mHolder.commentDate.setText(current.date);
            mHolder.comments.setText(current.comment);
            mHolder.commentRatingbar.setRating(current.rating);
        }
    }

    @Override
    public int getItemCount() {
        return data.size() + headerData.size() + commentsData.size() + 4;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            viewType = 0;
        } else if (position == 1) {
            viewType = 1;
        } else if (position == 2) {
            viewType = 2;
        } else if (position == 3) {
            viewType = 3;
        } else if (position == 4) {
            viewType = 4;
        } else if (position == 5) {
            viewType = 5;
        } else if (position == 6) {
            viewType = 6;
        } else if (position == 7) {
            viewType = 7;
        } else if (position == 8) {
            viewType = 8;
        } else if (position == 9) {
            viewType = 9;
        } else if (position == 10) {
            viewType = 10;
        } else if (position == 11) {
            viewType = 11;
        } else if (position == 12) {
            viewType = 12;
        } else if (position == 13) {
            viewType = 13;
        } else if (position == 14) {
            viewType = 14;
        }else if (position == 15) {
            viewType = 15;
        }


        return viewType;
    }

    class MyViewHolder extends MainViewHolder {
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.poiName);
            icon = (ImageView) itemView.findViewById(R.id.poiIcon);

        }

    }


    public class HeaderHolder extends MainViewHolder {
        TextView txtInfo;

        public HeaderHolder(View itemView) {
            super(itemView);
            this.txtInfo = (TextView) itemView.findViewById(R.id.txtinfo);

        }
    }

    public class GalleryHolder extends MainViewHolder {
        Gallery gallery;

        public GalleryHolder(View itemView) {
            super(itemView);
            this.gallery = (Gallery) itemView.findViewById(R.id.gallery);

        }
    }

    public class TapHolder extends MainViewHolder {
        RatingBar ratingBar;

        public TapHolder(View itemView) {
            super(itemView);
            this.ratingBar = (RatingBar) itemView.findViewById(R.id.tapToRate);
            this.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    Global.setRating = rating;
                    Intent intent = new Intent(context,Review.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });

        }
    }

    public class TitleHolder extends MainViewHolder {
        ImageView imgType,imgFavorite;
        TextView txtTitle;
        RatingBar ratingBar;

        public TitleHolder(View itemView) {
            super(itemView);
            imgType=(ImageView)itemView.findViewById(R.id.imgType);
            imgFavorite=(ImageView)itemView.findViewById(R.id.imgFavorite);
            txtTitle=(TextView)itemView.findViewById(R.id.txtTitle);
            ratingBar=(RatingBar)itemView.findViewById(R.id.ratingBar);
        }
    }

    public class RatingHolder extends MainViewHolder {
        TextView reviewTxt;
        RatingBar ratingBarReview;
        ProgressBar progressbarE, progressbarVG, progressbarAVG, progressbarP, progressbarT;

        public RatingHolder(View itemView) {
            super(itemView);
            this.reviewTxt = (TextView) itemView.findViewById(R.id.reviewTxt);
            this.ratingBarReview = (RatingBar) itemView.findViewById(R.id.ratingBarReview);
            this.progressbarE = (ProgressBar) itemView.findViewById(R.id.progressbarE);
            this.progressbarVG = (ProgressBar) itemView.findViewById(R.id.progressbarVG);
            this.progressbarAVG = (ProgressBar) itemView.findViewById(R.id.progressbarAVG);
            this.progressbarP = (ProgressBar) itemView.findViewById(R.id.progressbarP);
            this.progressbarT = (ProgressBar) itemView.findViewById(R.id.progressbarT);
        }
    }


    public class CommentsHolder extends MainViewHolder {
        ImageView commentPhoto;
        TextView commentTitle, commentDate, comments;
        RatingBar commentRatingbar;

        public CommentsHolder(View itemView) {
            super(itemView);
            this.commentPhoto = (ImageView) itemView.findViewById(R.id.commentPhoto);
            this.commentTitle = (TextView) itemView.findViewById(R.id.commentTitle);
            this.commentDate = (TextView) itemView.findViewById(R.id.commentDate);
            this.comments = (TextView) itemView.findViewById(R.id.comments);
            this.commentRatingbar = (RatingBar) itemView.findViewById(R.id.commentRatingbar);
        }
    }

    public class MainViewHolder extends RecyclerView.ViewHolder {
        public MainViewHolder(View itemView) {
            super(itemView);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

custom_row_rating.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/btn_white_press"
    android:clickable="true"
    android:orientation="vertical">


</LinearLayout>

btn_white_press.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#4b14b1"/>
    </shape>
</item>
<item>
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF"/>
    </shape>
</item>
</selector>