【问题标题】:Recyclerview displaying only the last json data?Recyclerview 只显示最后的 json 数据?
【发布时间】:2017-07-09 21:07:41
【问题描述】:

我正在尝试在 android 的回收站视图中显示数据。我但它一直只显示最后的数据,我不知道为什么。我似乎做的一切都是正确的。并且recycler view有一个view pager。

这是我的 我的适配器类

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();
    }
}

这是我的 mainactivty 类

 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。但我只是不明白我做错了什么。

【问题讨论】:

    标签: android arrays android-recyclerview


    【解决方案1】:

    我认为这与并发问题有关,请在设置适配器后尝试调用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();
            }
        }});
    
        }
    });
    

    【讨论】:

    • 试过你说的没用。每个项目的不同getter实例是什么意思。
    猜你喜欢
    • 2023-03-07
    • 2019-01-26
    • 2021-01-09
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多