如何从扩展RecyclerView适配器的类启动DialogFragment

时间:2016-03-02 08:15:14

标签: android android-volley

如何启动片段DialogFragment RecyclerView适配器 错误:android.support.v7.view.ContextThemeWrapper无法强制转换为android.app.Activity ?的onClick(ArticleAdapter.java:235)

片段

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
private ArrayList<BizForumArticleInfo> list = new ArrayList<>();
private LayoutInflater layoutInflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Context context;
private View mRemoveableView;
private RequestQueue requestQueue;
String CompID;

public ArticleAdapter(Context context) {
    layoutInflater = LayoutInflater.from(context);
    volleySingleton = VolleySingleton.getInstance();
    imageLoader = volleySingleton.getImageLoader();

    volleySingleton = VolleySingleton.getInstance();
    requestQueue = volleySingleton.getRequestQueue();

    MySharedPreferences prefs = MySharedPreferences.getInstance(context, SESSION);
    CompID = prefs.getString("CompID", "");
}

public void setList(ArrayList<BizForumArticleInfo> list) {
    this.list = list;
    notifyItemRangeChanged(0, list.size());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.adapter_biz_article, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    ViewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
    ViewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, ViewHolder.swipeLayout.findViewById(R.id.bottom_wrapper));
    ViewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
        @Override
        public void onClose(SwipeLayout layout) {
            //when the SurfaceView totally cover the BottomView.

        }

        @Override
        public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
            //you are swiping.


        }

        @Override
        public void onStartOpen(SwipeLayout layout) {

        }

        @Override
        public void onOpen(SwipeLayout layout) {

            //when the BottomView totally show.
        }

        @Override
        public void onStartClose(SwipeLayout layout) {

        }

        @Override
        public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            //M.i("==========xvel",""+xvel);
            //M.i("==========yvel",""+yvel);
            //when user's hand released.
        }
    });

    ViewHolder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    final BizForumArticleInfo current = list.get(position);
    holder.ArticleSum.setText(current.getArticleSum());
    holder.name.setText(current.getArticleCreatedBy());
    holder.TopicNm.setText(current.getTopicNm());
    holder.padLikes.setText(current.getLikes() + "");
    holder.titleImages.setText(current.getArticleTitle());
    final int artipost = current.getArticleID();
    final int artipostArticleOwnerUsrID = current.getArticleOwnerUsrID();
    final String articleId = current.getArticleID() + "";

    final String isFavourite = current.getIsFavourite();
    if (isFavourite.equals("yes")) {
        holder.favorites.setImageResource(R.drawable.fav_orange_icon);
        holder.favorites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                PatApi.isFavorites(articleId, "remove", "article");
                notifyDataSetChanged();
            }
        });
    } else if (isFavourite.equals("no")) {
        holder.favorites.setImageResource(R.drawable.fav_ic);
        holder.favorites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                PatApi.isFavorites(articleId, "add", "article");
                notifyDataSetChanged();
            }
        });
    }


    int LikeFlag = current.getLikeFlag();
    if (LikeFlag == 0) {
        holder.patClick.setImageResource(R.drawable.pat_grey_ic);
        /*int width = 30;
        int height = 30;
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
        holder.patClick.setLayoutParams(parms);*/
    }
    if (LikeFlag == 1) {
        holder.patClick.setImageResource(R.drawable.pat_greenn_ic);
        //holder.patClick.getLayoutParams().height = 20;
        /*int width = 30;
        int height = 30;
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
        holder.patClick.setLayoutParams(parms);*/

    }
    holder.patClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            PatApi.isPat(CompID, articleId, "3");
            notifyDataSetChanged();
            notifyItemChanged(position);
        }
    });

    String flag = current.getIsFavourite();
    if (flag.equals("no")) {
        holder.starRating.setVisibility(View.INVISIBLE);
    }
    if (flag.equals("yes")) {
        holder.starRating.setVisibility(View.VISIBLE);
    }

    Date ArticleCreatedDate = current.getArticleCreatedDt();
    if (ArticleCreatedDate != null) {
        String formattedDate = dateFormatter.format(ArticleCreatedDate);
        holder.ArticleCreatedDt.setText(formattedDate);
    }
    String thumb = current.getArticleThumb();
    String img = current.getArticleImg();
    String userImage = current.getUsrPicture();


    if (thumb != null) {
        holder.imageCard.setImageUrl(thumb, imageLoader);
    }
    if (userImage != null) {
        holder.userImg.setImageUrl(userImage, imageLoader);
    }
    holder.articleClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), ArticleFullView.class);
            i.putExtra("ArticleId", String.valueOf(artipost));
            i.putExtra("ArticleOwnerUsrID", String.valueOf(artipostArticleOwnerUsrID));
            i.putExtra("isFavourite", isFavourite);
            i.putExtra("patLikes", current.getLikes() + "");
            v.getContext().startActivity(i);
        }
    });
    holder.share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            FragmentManager fr = ((Activity) v.getContext()).getFragmentManager();
            ShareFragment msgDialog = new ShareFragment();
            msgDialog.show(fr, "Dialog");


        }
    });


}


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

public static class ViewHolder extends RecyclerView.ViewHolder {
    private TextView name, TopicNm, ArticleCreatedDt, ArticleSum, padLikes;
    ImageView imag, favorites, articleClick;
    NetworkImageView userImg, imageCard;
    ImageButton starRating, patClick, share;
    TextView titleImages;
    static SwipeLayout swipeLayout;

    public ViewHolder(View itemView) {
        super(itemView);

        swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
        name = (TextView) itemView.findViewById(R.id.name);
        TopicNm = (TextView) itemView.findViewById(R.id.TopicNm);
        ArticleCreatedDt = (TextView) itemView.findViewById(R.id.ArticleCreatedDt);
        ArticleSum = (TextView) itemView.findViewById(R.id.ArticleSum);
        userImg = (NetworkImageView) itemView.findViewById(R.id.userImg);//userImg
        padLikes = (TextView) itemView.findViewById(R.id.padLikes);
        starRating = (ImageButton) itemView.findViewById(R.id.starRating);
        patClick = (ImageButton) itemView.findViewById(R.id.patClick);
        imageCard = (NetworkImageView) itemView.findViewById(R.id.imag);
        articleClick = (ImageView) itemView.findViewById(R.id.articleClick);
        favorites = (ImageView) itemView.findViewById(R.id.favorites);
        titleImages = (TextView) itemView.findViewById(R.id.titleImages);
        share = (ImageButton) itemView.findViewById(R.id.share);
    }

 }
}

DialogFragment

public class ShareFragment extends DialogFragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
VolleySingleton volleySingleton;
private RequestQueue requestQueue;
TextView share;
EditText To, message;

public ShareFragment() {
    // Required empty public constructor
}

public static ShareFragment newInstance(String param1, String param2) {
    ShareFragment fragment = new ShareFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    volleySingleton = VolleySingleton.getInstance();
    requestQueue = volleySingleton.getRequestQueue();
}

public void postCompanyArticle(final int limit) {

    StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfigApi.ShareArticle(), new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            M.i("==================SHARE", "" + response);

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

            String msg = VolleyException.getErrorMessageFromVolleyError(error);

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("content_id", "2725");
            params.put("content_type", "article");
            params.put("message", "testing user");
            params.put("share_with", "yogesh");
            params.put("limit", String.valueOf(limit));

            return params;
        }
    };


    stringRequest.setRetryPolicy(
            new DefaultRetryPolicy(3 * 1000, 1, 1.0f));
    requestQueue.add(stringRequest);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_share, container, false);
    share = (TextView) view.findViewById(R.id.overCmt);
    To = (EditText) view.findViewById(R.id.to);
    message = (EditText) view.findViewById(R.id.message);
    //  postCompanyArticle(10);

    return view;
  }

}                                                      

1 个答案:

答案 0 :(得分:1)

初始化适配器中的上下文

public ArticleAdapter(Context context) {
    layoutInflater = LayoutInflater.from(context);
    volleySingleton = VolleySingleton.getInstance();
    imageLoader = volleySingleton.getImageLoader();

    volleySingleton = VolleySingleton.getInstance();
    requestQueue = volleySingleton.getRequestQueue();

    MySharedPreferences prefs = MySharedPreferences.getInstance(context, SESSION);
    CompID = prefs.getString("CompID", "");
    this.context=context;
}

并用此

替换你的onclick
holder.share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            FragmentManager fr = ((Activity)context).getFragmentManager();
            ShareFragment msgDialog = new ShareFragment();
            msgDialog.show(fr, "Dialog");


        }
    });