如何在RecyclerView viewHolder onClick中更改Fragment中的文本(在recyclerVIew外部)

时间:2017-09-30 13:49:46

标签: java android android-recyclerview fragment

My cart

这就是我需要的。 删除一个项目时需要更新总值。 我设法删除该项目,并在适配器视图中以json respose的形式获取总值。不知道如何在Fragment中设置更新文本。

这是我的购物车碎片 Cart.java 公共类推车扩展片段{

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

Context context;
Activity activity;
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;

String GET_JSON_DATA_HTTP_URL = "http://192.168.0.106/slbros/index.php/get/cart?p_d_id=12&lan=en";
String Total_URL = "http://192.168.0.106/slbros/index.php/get/total?p_d_id=12";
String JSON_CDID = "cart_dtl_id";
String JSON_IMG_URL = "img_url";
String JSON_QTY = "qty";
String JSON_NAME = "name";
String JSON_UNIT = "unit";
String JSON_PRICE = "price";
String JSON_P_ID = "product_id";


JsonArrayRequest jsonArrayRequest;
RequestQueue requestQueue;
ProgressBar progressBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_cart, container, false);

    TextView total_tv = (TextView)v.findViewById(R.id.total_payment_value);
    //recycler view
    GetDataAdapter1 = new ArrayList<>();

    recyclerView = (RecyclerView) v.findViewById(R.id.rv_cart_list);
    progressBar = (ProgressBar) v.findViewById(R.id.progressBar2);

    recyclerView.setHasFixedSize(true);

    recyclerViewlayoutManager = new LinearLayoutManager(getContext());

    recyclerView.setLayoutManager(recyclerViewlayoutManager);

    progressBar.setVisibility(View.VISIBLE);

    JSON_DATA_WEB_CALL();

    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Cart");
}

public void JSON_DATA_WEB_CALL() {

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    progressBar.setVisibility(View.GONE);
                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(getContext(), "some error....", Toast.LENGTH_SHORT).show();
                }
            }
    );

    requestQueue = Volley.newRequestQueue(getContext());

    jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(60000, 0, 1));
    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {

    for (int i = 0; i < array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);

            GetDataAdapter2.setImg_url(json.getString(JSON_IMG_URL));
            GetDataAdapter2.setName(json.getString(JSON_NAME));
            GetDataAdapter2.setPrice(json.getInt(JSON_PRICE));
            GetDataAdapter2.setProduct_id(json.getInt(JSON_P_ID));
            GetDataAdapter2.setCart_dtl_id(json.getInt(JSON_CDID));
            GetDataAdapter2.setProduct_qty(json.getInt(JSON_QTY));
        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }

    recyclerViewadapter = new RecyclerViewAdapterCart(GetDataAdapter1, getContext());

    recyclerView.setAdapter(recyclerViewadapter);
}}

这是我的适配器 RecyclerViewAdapterCart.java

public class RecyclerViewAdapterCart extends RecyclerView.Adapter<RecyclerViewAdapterCart.ViewHolder> {

JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
String baseURL = "http://192.168.0.106/slbros/index.php/";
Context context;
Activity activity;
List<GetDataAdapter> getDataAdapter;

ImageLoader imageLoader1;

public RecyclerViewAdapterCart(List<GetDataAdapter> getDataAdapter, Context context) {

    super();
    this.getDataAdapter = getDataAdapter;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_list_item, parent, false);

    ViewHolder viewHolder = new ViewHolder(v);

    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {

    GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);

    imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

    imageLoader1.get(getDataAdapter1.getImg_url(),
            ImageLoader.getImageListener(
                    Viewholder.product_image_view,//Server Image
                    R.mipmap.ic_launcher,//Before loading server image the default showing image.
                    android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
            )
    );

    Viewholder.product_image_view.setImageUrl(getDataAdapter1.getImg_url(), imageLoader1);
    Viewholder.product_name_TextView.setText(getDataAdapter1.getName()+" - "+String.valueOf(getDataAdapter1.getProduct_qty()));
    Viewholder.product_price_qty_TextView.setText(getDataAdapter1.getPrice()+".00 Rs X "+String.valueOf(getDataAdapter1.getProduct_qty()));
    Viewholder.product_t_price_TextView.setText(String.valueOf(getDataAdapter1.getProduct_qty()*getDataAdapter1.getPrice())+".00 Rs");
    Viewholder.product_id_TextView.setText(String.valueOf(getDataAdapter1.getProduct_id()));
    Viewholder.cart_dtl_id_TextView.setText(String.valueOf(getDataAdapter1.getCart_dtl_id()));
}

@Override
public int getItemCount() {

    return getDataAdapter.size();
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public NetworkImageView product_image_view;
    public TextView product_name_TextView;
    public TextView product_price_qty_TextView;
    public TextView product_t_price_TextView;
    public TextView product_id_TextView;
    public TextView cart_dtl_id_TextView;
    public Button p_cancel_btn;


    public ViewHolder(View itemView) {

        super(itemView);

        product_image_view = (NetworkImageView) itemView.findViewById(R.id.item_image1);
        product_name_TextView = (TextView) itemView.findViewById(R.id.product_name);
        product_price_qty_TextView = (TextView) itemView.findViewById(R.id.p_qty_price);
        product_t_price_TextView = (TextView) itemView.findViewById(R.id.p_t__price);
        product_id_TextView = (TextView) itemView.findViewById(R.id.product_id_tv);
        cart_dtl_id_TextView = (TextView) itemView.findViewById(R.id.cart_dtl_id_tv);
        p_cancel_btn = (Button)itemView.findViewById(R.id.p_cancel_btn);

        // set item view
        p_cancel_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(final View v) {
        String str = product_name_TextView.getText().toString();
        //Toast.makeText(v.getContext(), str, Toast.LENGTH_SHORT).show();
        String cart_dtl_id = cart_dtl_id_TextView.getText().toString();

        String url = baseURL + "delete/select?cart_dtl_id="+cart_dtl_id;

        jsonArrayRequest = new JsonArrayRequest(url,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        //progressBar.setVisibility(View.GONE);
                        Toast.makeText(v.getContext(), "response -- " + response, Toast.LENGTH_LONG).show(); 





// here, I have to change the total TextView in Fragment
// Total_TextView.setText("1000rs")




                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(v.getContext(), "error", Toast.LENGTH_LONG).show();
                    }
                }
        );

        requestQueue = Volley.newRequestQueue(v.getContext());
        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(60000,0,1));
        requestQueue.add(jsonArrayRequest);

        getDataAdapter.remove(getAdapterPosition());
        notifyItemRemoved(getAdapterPosition());
        notifyItemRangeChanged(getAdapterPosition(),getDataAdapter.size());

    }
}}

先谢谢

1 个答案:

答案 0 :(得分:0)

添加到ReyclerView。

private ItemsChangedListener itemsChangedListener;

public interface ItemsChangedListener {
    void onItemsChanged(int sum);

}

public void setItemsChangedListener(ItemsChangedListener listener) {
 this.itemsChangedListener = listener;

}

 new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //progressBar.setVisibility(View.GONE);
                   // Calc here the Value if you can.
                  if(listener != null) listener.onItemsChanged(sum);
  }

片段     public class Cart extends Fragment实现ItemsChangedListener     recyclerView.setItemsChangedListener(this);

@override
public void onItemsChanged(int sum) {
   //Update TextView    
}