使用Recyclerview

时间:2017-07-26 16:03:52

标签: java android arraylist android-recyclerview adapter

在用户点击项目的时候想用呼叫api在arraylist设定值但是在使用Recyclerview的时候有问题,在使用Listview的时候变更工作! (但我想使用Recyclerview)

我收到错误

  

java.lang.IndexOutOfBoundsException:索引0无效,大小为0

并在行

  

timelineFeedListModels.get(new_position).getPost()getPoll()得到(位置).setVote(的Integer.parseInt(表决));

但是当我更改为Listview时不是错误

recyclerView = (RecyclerView) lay_custom_center_post_topic.findViewById(R.id.lv_poll_feed);

 AdapDetailTopicPoll adapDetailTopicPoll = new AdapDetailTopicPoll(timelineFeedListModels.get(i).getPost().getPoll());
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(adapDetailTopicPoll);
            adapDetailTopicPoll.notifyDataSetChanged();

            new_position = i;


 public void sendVodePoll(final int position, String poll_id,final int new_position) {

        RequestParams params = new RequestParams();
        String URL = "xxxx/xx";
        params.put("poll_id", poll_id);

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(URL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                ArrayList<ProfileFeedListModelTwo.PostBean.PollBean> pollBeanArrayList = new ArrayList<ProfileFeedListModelTwo.PostBean.PollBean>();
                StringBuilder result = new StringBuilder();
                String vote = null;
                String poll_id=null;
                String status_vote=null;
                Gson gson = new Gson();
                try {
                    List<TopicVoteModel> topicVoteModels = new ArrayList<>();
                    for (int i=0 ; i<response.length() ; i++){
                        JSONObject jsonObject = response.getJSONObject(i);
                        vote = jsonObject.getString("vote");
                        topicVoteModels.add(gson.fromJson(String.valueOf(jsonObject), TopicVoteModel.class));
                    }

                    boolean new_status_vote = Boolean.parseBoolean(status_vote);

                    timelineFeedListModels.get(new_position).getPost().getPoll().get(position).setVote(Integer.parseInt(vote)); ////line to error 
                    timelineFeedListModels.get(new_position).getPost().getPoll().get(position).setStatus_vote(new_status_vote);

                    for (ProfileFeedListModelTwo.PostBean.PollBean pollBean : timelineFeedListModels.get(new_position).getPost().getPoll()){
                        pollBeanArrayList.add(pollBean);
                    }
                    for (int i=0 ; i<pollBeanArrayList.size() ; i++){

                    }

            AdapDetailTopicPoll adapDetailTopicPoll = new AdapDetailTopicPoll(pollBeanArrayList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(adapDetailTopicPoll);
            adapDetailTopicPoll.notifyDataSetChanged();

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

和我的适配器

public class AdapDetailTopicPoll extends RecyclerView.Adapter<AdapDetailTopicPoll.MyViewHolder> {

    SearchDetailFragment searchDetailFragment = new SearchDetailFragment();
    Context context;
    private List<ProfileFeedListModelTwo.PostBean.PollBean> poll = new ArrayList<ProfileFeedListModelTwo.PostBean.PollBean>();

    public AdapDetailTopicPoll( List<ProfileFeedListModelTwo.PostBean.PollBean> poll ){
        this.poll = poll;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.custom_profile_feed_topic_poll,parent,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tx_poll.setText(""+poll.get(position).getName());
        if (poll.get(position).isStatus_vote()==false){
            holder.img_vote.setImageResource(R.drawable.vote_t_1);
            holder.tx_count_vote.setText(""+poll.get(position).getVote());

        }else {
            holder.img_vote.setImageResource(R.drawable.vote_t_2);
            holder.tx_count_vote.setText(""+poll.get(position).getVote());

        }


    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tx_poll,tx_count_vote;
        public ImageView img_vote;

        public MyViewHolder(View view) {
            super(view);
            tx_poll = (TextView) view.findViewById(R.id.tx_poll);
            tx_count_vote = (TextView) view.findViewById(R.id.tx_count_vote);
            img_vote = (ImageView) view.findViewById(R.id.img_vote);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ProfileFragment.poll_id = String.valueOf(poll.get(getLayoutPosition()).getId());
                    searchDetailFragment.sendVodePoll(getLayoutPosition(),ProfileFragment.poll_id,SearchDetailFragment.new_position);
                }
            });
        }
    }
}

是我在适配器中设置OnClick的问题吗?

如何解决?

我的模特

public static class PollBean {

            private int id;
            private int timeline_id;
            private String name;
            private int vote;
            private String created_at;
            private String updated_at;
            private boolean status_vote;

            public PollBean(int id, boolean status_vote, String name, int vote) {
                this.id=id;
                this.status_vote=status_vote;
                this.name=name;
                this.vote=vote;


            }

            public boolean isStatus_vote() {
                return status_vote;
            }

            public void setStatus_vote(boolean status_vote) {
                this.status_vote = status_vote;
            }

            public int getId() {
                return id;
            }

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

            public int getTimeline_id() {
                return timeline_id;
            }

            public void setTimeline_id(int timeline_id) {
                this.timeline_id = timeline_id;
            }

            public String getName() {
                return name;
            }

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

            public int getVote() {
                return vote;
            }

            public void setVote(int vote) {
                this.vote = vote;
            }

            public String getCreated_at() {
                return created_at;
            }

            public void setCreated_at(String created_at) {
                this.created_at = created_at;
            }

            public String getUpdated_at() {
                return updated_at;
            }

            public void setUpdated_at(String updated_at) {
                this.updated_at = updated_at;
            }
        }

0 个答案:

没有答案