改造:回调不会访问成功和失败方法

时间:2015-05-18 10:35:15

标签: android callback baseadapter retrofit

我正在尝试使用Retrofit进行HTTP-POST。 我已经在其他课程中成功完成了这项工作,但现在它无效。也许是因为我像以前一样扩展BaseAdapter而不是Activity或Fragment,但是当我调用POST方法时,它甚至无法访问成功和/或失败方法

public class AsmReactionsAdapter extends BaseAdapter implements Callback<Like> {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final AnswersAsm answer = getItem(position);
        ViewHolder viewHolder;
        if (convertView != null) {
            viewHolder = (ViewHolder) convertView.getTag();
        } else {
            convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }

        viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like(answer.getAnswerId());
            }
        });

        return convertView;
    }

   private void like(int id)
    {
        Like like=new Like();
        like.setGebruikerId(3);
        like.setAntwoordDossierId(id);
        like.setProvider("LOCAL");
        JppApplication.getService().likeAntwoord(like, this);

    }

    @Override
    public void success(Like like, Response response) {
        System.out.println("werkt");
    }

    @Override
    public void failure(RetrofitError retrofitError) {
        System.out.println("werkt niet");
    }

    static class ViewHolder {
        Button likeBtn;
        public ViewHolder(View view) {  
            likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);        
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。这是解决方案

 public class AsmReactionsAdapter extends BaseAdapter{
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final AnswersAsm answer = getItem(position);
            ViewHolder viewHolder;
            if (convertView != null) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = from(context).inflate(R.layout.asm_reaction_item, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }

            viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                Like like=new Like();
                like.setId(3);
                like.setDossierId(answer.getAnswerId());
                like.setProvider("LOCAL");
                    JppApplication.getService().likeAntwoord(like, new Callback<Like(){
                        @Override
                        public void success(Like like, Response response) {
                            System.out.println("LIKE OK");
                        }

                        @Override
                        public void failure(RetrofitError retrofitError) {
                            System.out.println("LIKE FAILURE");
                        }
                    });
                }
            });

            return convertView;
        }
    }

        static class ViewHolder {
            Button likeBtn;
            public ViewHolder(View view) {  
                likeBtn=(Button) view.findViewById(R.id.asm_reaction_item_btnAsmLike);        
            }
        }