Recyclerview仅显示最后的json数据?

时间:2017-07-09 12:59:24

标签: android arrays android-recyclerview

我正在尝试在android中的回收站视图中显示数据。我,但它只显示最后的数据,我不知道为什么。我似乎正在做一切正确。回收者视图有一个视图寻呼机。

这是我的 Myadapter类

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    Context c;
    List<Getter> getter;

    public MyAdapter(List<Getter> getter, Context c) {
        super();
        this.c = c;
        this.getter = getter;
    }

    //INITIALIZE VIEWHODER
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //VIEW OBJ
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null);


        //HOLDER
        MyHolder holder=new MyHolder(v);

        return holder;
    }


    //BIND VIEW TO DATA
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        String images[] = {getter.get(position).getUrl1(), getter.get(position).getUrl2()};
        holder.gamename.setText(getter.get(position).getGame_name());
        holder.cost.setText(getter.get(position).getCost());
        holder.usname.setText(getter.get(position).getFull_name());
        Picasso.with(c)
                .load(getter.get(position).getPicture())
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .transform(new CircleTransform())
                .fit()
                .into(holder.usimage);

        CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(c,images);
        holder.mViewPager.setAdapter(mCustomPagerAdapter);

        holder.indicator.setViewPager(holder.mViewPager);

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                Snackbar.make(v,getter.get(pos).getGame_name(),Snackbar.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return getter.size();
    }
}

这是我的主要活动类

 public class All_timeline extends Fragment {
        SqlHandler sqlHandler;
        RecyclerView rv;
        MyAdapter adapter;
        private List<Getter> getter;

        public All_timeline() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View x = inflater.inflate(R.layout.all_timeline, container, false);
            sqlHandler = new SqlHandler(getActivity());
            //recycler
            rv = (RecyclerView) x.findViewById(R.id.mRecycler);
            getter= new ArrayList<>();
            rv.setLayoutManager(new LinearLayoutManager(getActivity()));

            rv.setItemAnimator(new DefaultItemAnimator());
           rv.addItemDecoration(new SimpleDividerItemDecoration(getResources()));

            registerUser("arinzeaco@gmail.com");

            adapter=new MyAdapter(getter,getActivity());
            rv.setAdapter(adapter);
            return x;
        }


        public void registerUser(final String email) {

            OkHttpClient client = new OkHttpClient();
            RequestBody body = new FormBody.Builder()
                    .add("username", email)
                    .build();
            Request request = new Request.Builder().url("http://www.thethinker.com.ng/gamer/allpost.php").post(body).build();
            Call call = client.newCall(request);
            call.enqueue(new Callback() {

                @Override
                public void onFailure(Call call, IOException e) {
                    Toast.makeText(getActivity(), "Registration failed",
                            Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final String myResponse = response.body().string();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            JSONObject jso;
                            Getter contactListItems = new Getter();
                            try {
                                jso = new JSONObject(myResponse);
                                JSONArray categories = jso.getJSONArray("posts");

                                for (int i = 0; i < categories.length(); i++) {
                                    String image1 = categories.getJSONObject(i).getString("image_1");
                                    String image2 = categories.getJSONObject(i).getString("image_2");
                                    String username = categories.getJSONObject(i).getString("username");
                                    String video_url = categories.getJSONObject(i).getString("video_url");
                                    String liked = categories.getJSONObject(i).getString("liked");
                                    String cost = categories.getJSONObject(i).getString("cost");
                                    String following = categories.getJSONObject(i).getString("following");
                                    String game_name = categories.getJSONObject(i).getString("game_name");
                                    String full_name = categories.getJSONObject(i).getString("full_name");
                                    String userimage = categories.getJSONObject(i).getString("userimage");

                                    contactListItems.setUsername(username);
                                    contactListItems.setGame_name(game_name);
                                    contactListItems.setCost(cost);
                                    contactListItems.setFollowing(following);
                                    contactListItems.setLiked(liked);
                                    contactListItems.setFull_name(full_name);
                                    contactListItems.setVideo(video_url);
                                    contactListItems.setUrl1(image1);
                                    contactListItems.setUrl2(image2);
                                    contactListItems.setPicture(userimage);

                                    getter.add(contactListItems);


                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }});

                        }
                    });



        }
    }

我还检查了其他stackoverflow问题,例如RecyclerView displaying the last item in the adapter several times. Need all adapter items to show in RecyclerView。但我只是不明白我做错了什么。

1 个答案:

答案 0 :(得分:0)

我认为它与并发问题有关,请在设置适配器后尝试调用http方法,并且在插入所有数据后不要忘记调用notifyDataSetChanged()。再想一想,确保为每个项目插入不同的Getter实例

例如在onCreateView

adapter=new MyAdapter(getter,getActivity()); rv.setAdapter(adapter); registerUser("arinzeaco@gmail.com");

onResponse中的

 @Override
 public void onResponse(Call call, Response response) throws IOException {
 final String myResponse = response.body().string();
 getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        JSONObject jso;
        try {
            jso = new JSONObject(myResponse);
            JSONArray categories = jso.getJSONArray("posts");

            for (int i = 0; i < categories.length(); i++) {
                Getter contactListItems = new Getter();
                String image1 = categories.getJSONObject(i).getString("image_1");
                String image2 = categories.getJSONObject(i).getString("image_2");
                String username = categories.getJSONObject(i).getString("username");
                String video_url = categories.getJSONObject(i).getString("video_url");
                String liked = categories.getJSONObject(i).getString("liked");
                String cost = categories.getJSONObject(i).getString("cost");
                String following = categories.getJSONObject(i).getString("following");
                String game_name = categories.getJSONObject(i).getString("game_name");
                String full_name = categories.getJSONObject(i).getString("full_name");
                String userimage = categories.getJSONObject(i).getString("userimage");

                contactListItems.setUsername(username);
                contactListItems.setGame_name(game_name);
                contactListItems.setCost(cost);
                contactListItems.setFollowing(following);
                contactListItems.setLiked(liked);
                contactListItems.setFull_name(full_name);
                contactListItems.setVideo(video_url);
                contactListItems.setUrl1(image1);
                contactListItems.setUrl2(image2);
                contactListItems.setPicture(userimage);

                getter.add(contactListItems);
            }
            adapter.notifyDataSetChanged();

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }});

    }
});