|
|
|
@ -5,17 +5,21 @@ import android.app.FragmentTransaction;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Message;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.GridView;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
|
|
|
|
import com.example.catapp.R;
|
|
|
|
|
import com.example.catapp.activity.IndexActivity;
|
|
|
|
|
import com.example.catapp.adapter.ProductAdapter;
|
|
|
|
|
import com.example.catapp.adapter.ShoppingCartAdapter;
|
|
|
|
|
import com.example.catapp.entity.CatProductPack;
|
|
|
|
@ -34,23 +38,50 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
|
|
|
|
|
private ProductFragment productFragment;
|
|
|
|
|
private GridView productGridView;
|
|
|
|
|
private TextView usernameText;
|
|
|
|
|
private Integer userId=2;
|
|
|
|
|
Button left1;
|
|
|
|
|
private Integer userId=13;
|
|
|
|
|
private List<CatProductPack> catProductList= new ArrayList<>();
|
|
|
|
|
Handler h=null;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
//给购物车集合赋值,用以判断界面的展示
|
|
|
|
|
cartList=IndexActivity.cartList;
|
|
|
|
|
Log.d("ShoppingCartFragment onCrate init cartList",""+cartList);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
// super.onActivityCreated(savedInstanceState);
|
|
|
|
|
// productGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
|
// @Override
|
|
|
|
|
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
|
// FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
|
|
|
|
// if (productFragment == null) {
|
|
|
|
|
// productFragment = new ProductFragment();
|
|
|
|
|
// }
|
|
|
|
|
// transaction.replace(R.id.main_content0, productFragment);
|
|
|
|
|
// transaction.commit();
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
@Override
|
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
|
|
|
View view;
|
|
|
|
|
// if (cartList.size() > 0) {
|
|
|
|
|
// //有商品
|
|
|
|
|
cartList=IndexActivity.cartList;
|
|
|
|
|
if (cartList.size() > 0) {
|
|
|
|
|
//有商品
|
|
|
|
|
view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_have_product, container, false);
|
|
|
|
|
// } else {
|
|
|
|
|
// view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false);
|
|
|
|
|
// }
|
|
|
|
|
linkHttp(view);
|
|
|
|
|
} else {
|
|
|
|
|
view = LayoutInflater.from(getActivity()).inflate(R.layout.cart_no_product, container, false);
|
|
|
|
|
}
|
|
|
|
|
init(view);
|
|
|
|
|
check();
|
|
|
|
|
// if (cartList.size() > 0)
|
|
|
|
|
linkHttp();
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -59,12 +90,15 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
|
|
|
|
|
*/
|
|
|
|
|
private void init(View view) {
|
|
|
|
|
usernameText=view.findViewById(R.id.username);
|
|
|
|
|
productGridView = view.findViewById(R.id.cart_productList);
|
|
|
|
|
//productGridView = view.findViewById(R.id.cart_productList);
|
|
|
|
|
if (cartList.size() > 0) {
|
|
|
|
|
|
|
|
|
|
// walkButton = view.findViewById(R.id.random_search);
|
|
|
|
|
// walkButton.setOnClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
walkButton = view.findViewById(R.id.random_search);
|
|
|
|
|
walkButton.setOnClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//productGridView = view.findViewById(R.id.cart_productList);
|
|
|
|
|
setData();
|
|
|
|
|
}
|
|
|
|
|
private void setData() {
|
|
|
|
@ -75,7 +109,9 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if (cartList.size() > 0) {}
|
|
|
|
|
if (cartList.size() > 0) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
switch (v.getId()) {
|
|
|
|
|
case R.id.random_search:
|
|
|
|
@ -91,29 +127,11 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*发送网络请求当前用户的购物车信息
|
|
|
|
|
*/
|
|
|
|
|
public void check(){
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
|
|
|
|
|
try {
|
|
|
|
|
// catProductList= okHttpShoppingcart.getProductPack(userId);
|
|
|
|
|
System.out.println(cartList.size() );
|
|
|
|
|
cartList=okHttpShoppingcart.getShoppingCartPack(userId);
|
|
|
|
|
System.out.println(cartList.get(0).getNum());
|
|
|
|
|
System.out.println(cartList.size());
|
|
|
|
|
System.out.println(cartList.size());
|
|
|
|
|
System.out.println(cartList.size());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
public void linkHttp(){
|
|
|
|
|
public void linkHttp(View view){
|
|
|
|
|
h =new Handler() {
|
|
|
|
|
@Override
|
|
|
|
|
public void handleMessage(@NonNull Message msg) {
|
|
|
|
@ -128,8 +146,9 @@ public class ShoppingCartFragment extends Fragment implements View.OnClickListen
|
|
|
|
|
public void run() {
|
|
|
|
|
OkHttpShoppingcart okHttpShoppingcart=new OkHttpShoppingcart();
|
|
|
|
|
try {
|
|
|
|
|
productGridView = view.findViewById(R.id.cart_productList);
|
|
|
|
|
catProductList= okHttpShoppingcart.getProductPack(userId);
|
|
|
|
|
//cartList=okHttpShoppingcart.getShoppingCartPack(userId);
|
|
|
|
|
cartList=okHttpShoppingcart.getShoppingCartPack(userId);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|