我有一个RecyclerView适配器类:
public class AdapterForRecyclerView extends RecyclerView.Adapter<ViewHolder> implements SuccessInterface{
Context context;
JSONArray sJsonArray;
private SuccessInterface sAdapterInterface;
public AdapterForRecyclerViewContext context, JSONArray expressJsonObject) {
this.context = context;
this.sJsonArray= expressJsonObject;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.card_list_parent, false);
return new ViewHolder (view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setIsRecyclable(false);
try {
JSONObject jsonObject = sJsonArray.getJSONObject(position);
String restaurantNameString = jsonObject.getString("restaurant_name");
} catch (JSONException e) {
Log.d("Error in:", "Dine Exp Adapter");
}
}
@Override
public int getItemCount() {
return sJsonArray.length();
}
//This is the method that I want to execute from Fragment
@Override
public void onSubmittedSuccessfully(int i) {
try {
Log.d("Reply update Adapter", String.valueOf(i));
JSONObject abc = sJsonArray.getJSONObject(i);
JSONArray jsonArrayObject = abc.getJSONArray("replies");
JSONObject jsonObject = new JSONObject();
jsonObject.put("review", "submit");
jsonArrayObject.put(jsonObject);
abc.put("replies", jsonArrayObject);
sJsonArray.put(i,abc);
Log.d("Reply updat obj", sJsonArray.getJSONObject(i).toString());
notifyItemChanged(i);
} catch (JSONException e) {
Log.d("Reply Update", e.toString());
e.printStackTrace();
}
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public LinearLayout itemLinearLayout, restaurantLinearLayout, checkinLinearLayout, newRestaurantLinearLayout;
public ViewHolder(View itemView) {
super(itemView);
itemLinearLayout = (LinearLayout) itemView.findViewById(R.id.ll_item_layout_card_list_dine_express_fragment);
restaurantLinearLayout = (LinearLayout) itemView.findViewById(R.id.ll_restaurant_layout_card_list_dine_express_fragment);
checkinLinearLayout = (LinearLayout) itemView.findViewById(R.id.ll_checkin_layout_card_list_dine_express_fragment);
newRestaurantLinearLayout = (LinearLayout) itemView.findViewById(R.id.ll_new_restaurant_layout_card_list_dine_express_fragment);
newRestaurantLinearLayout.setOnClickListener(this);
replyLinearLayout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
}
}
}
public interface ExpressAdapterInterface {
void replyFragmentStarted(String s, int i);
}
}
SuccessInterface在片段中定义,如:
public class RepliesFragment extends Fragment implements View.OnClickListener {
private ReplySubmitSuccessInterface replySubmitSuccessInterface;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
replySubmitSuccessInterface = (ReplySubmitSuccessInterface) getContext();
replySubmitSuccessInterface.replySubmittedSuccessfully(i);
}
public interface ReplySubmitSuccessInterface {
void replySubmittedSuccessfully(int i);
}
}
在上面的适配器中我没有使用正确的上下文实现此方法。这是正确的方法还是有任何解决方法?
感谢任何帮助!