单击baseadapter按钮时,选项卡会刷新

时间:2015-12-22 17:06:50

标签: java android android-fragments tabs fragment

我是片段概念的新手我正在使用选项卡布局,其中一个选项卡由列表视图组成,我使用基本适配器来描述列表行,我想在那里刷新第二个选项卡点击第一个标签的按钮。我已经尝试了片段交易方法,但它没有做我想要的工作请在这里帮忙

这是baseadapter

public class Second_adapter extends BaseAdapter {
    Context context;
    Activity activity;
    TextView basename, SubItemprice, itemdesc, SubItemdesc;
    LayoutInflater inflater;
    ImageView order;
    private List<JSONParser> items;
    String itembasename, itemde, subtmsub, subitempri;
    SharedPreferences.Editor loginPrefsEditor;

    public Second_adapter(Context context, List<JSONParser> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null)
            view = inflater.inflate(R.layout.item_clicked, viewGroup, false);
        basename = (TextView) view.findViewById(R.id.basename);
        SubItemprice = (TextView) view.findViewById(R.id.SubItemprice);
        itemdesc = (TextView) view.findViewById(R.id.itemdesc);
        SubItemdesc = (TextView) view.findViewById(R.id.SubItemdesc);
        order = (ImageView) view.findViewById(R.id.order);
        final JSONParser setdata = items.get(i);
        basename.setText(setdata.getBaseName());
        itemdesc.setText(setdata.getItemdesc());
        SubItemdesc.setText(setdata.getSubItemdesc());
        SubItemprice.setText(setdata.getSubItemprice());
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itembasename = setdata.getBaseName();
                itemde = setdata.getItemdesc();
                subtmsub = setdata.getSubItemdesc();
                subitempri = setdata.getSubItemprice();
                Session userloggedin = new Session(context);
                if (userloggedin.isLoggedIn()) {
                    addcart();
                    Cart cart = new Cart();
                    FragmentManager fragmentManager = activity.getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.cartfragment, cart);
                    Toast.makeText(context, "LoggedIn"+ itembasename+itemde+subtmsub+subitempri, Toast.LENGTH_SHORT).show( );
                 }else{
                    Toast.makeText(context,"Please Login", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return view;
    }

    private void addcart() {
        SharedPreferences customerid=context.getSharedPreferences("loginPrefs",Context.MODE_PRIVATE);
          String customid=customerid.getString("customerid","");
        String addcarturl = "http://standardtakeaway.co.uk/json/cart_ p rocess.p h p?userid = "+customid+" & Item=" + itembasename+itemde+subtmsub+" & Itemcount=1&price="+subitempri;
         Log.d("Cart",addcarturl);
        JsonArrayRequest cartreq=new JsonArrayRequest(Request.Method.POST, addcarturl, (String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                     for (int i=0; i<response.length();i++){
                    try {
                          JSONObject cartobj=response.getJSONObject(i);
//                        String count=cartobj.getString("count");
//                        loginPrefsEditor.putString("Badge",count);
//                        loginPrefsEditor.commit();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,""+error, Toast.LENGTH_SHORT).show();
            }
        });
        AppController.getInstance().addToRequestQueue(cartreq);
    }
}

这是我想要刷新的另一个标签

public class Cart extends Fragment {
    Button check_out;
    TextView subtotal;
    ListView cartview;
    CartAdapter cartAdapter;
    List<JSONParser> cartitems;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View cartfrag = inflater.inflate(R.layout.cart, container, false);
        subtotal = (TextView) cartfrag.findViewById(R.id.subtotal);
        cartview = (ListView) cartfrag.findViewById(R.id.cartview);
        cartitems = new ArrayList<JSONParser>();
        cartAdapter = new CartAdapter(getActivity(), cartitems);
        cartview.setAdapter(cartAdapter);
        check_out = (Button) cartfrag.findViewById(R.id.check_out);
        check_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent cart_check = new Intent(getActivity(), Check.class);
                startActivity(cart_check);
            }
        });

        cartdata();
        return cartfrag;
    }

    void cartdata() {
        SharedPreferences customerid=getActivity().getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
        String customid=customerid.getString("customerid","");
        String carturl = "http://standardtakeaway.co.uk/json/view_cart.php?userid=" + customid;
        Log.d("CartData", carturl);
        JsonObjectRequest cartreq = new JsonObjectRequest(Request.Method.GET, carturl, (String) null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray objarray = response.getJSONArray("items");
                    for (int i = 0; i < objarray.length(); i++) {
                        JSONObject cartdataobj = objarray.getJSONObject(i);
                        JSONParser parserdata = new JSONParser();
                        parserdata.setCartquantity(cartdataobj.getString("qty"));
                        parserdata.setCartbase(cartdataobj.getString("BaseName"));
                        parserdata.setCartprice(cartdataobj.getString("price"));
                        cartitems.add(parserdata);
                    }
                    JSONObject subobj=response.getJSONObject("details");
                    String subtext=subobj.getString("subtotal");
                    subtotal.setText(subtext);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                cartAdapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getActivity(), "" + error, Toast.LENGTH_SHORT).show();
            }
        });
        cartreq.setRetryPolicy(new DefaultRetryPolicy(6000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(cartreq);
    }

    public static Cart getInstance(String message) {
        Cart cart = new Cart();
        Bundle bundle = new Bundle();
        bundle.putString("MSG", message);
        cart.setArguments(bundle);
        return cart;
    }
}

0 个答案:

没有答案