仿京東的購物車和訂單

//從購物車跳轉到了確認訂單界面

/**

* 確認下單界面
*/
public class Main2Activity extends AppCompatActivity {
private TextView text_order;
private TextView text_kuan;
private ImageView tu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Intent intent = getIntent();

final CountPriceBean countPriceBean = (CountPriceBean) intent.getSerializableExtra("order");

text_kuan = (TextView) findViewById(R.id.text_kuan);
text_order = (TextView) findViewById(R.id.text_order);
tu = (ImageView) findViewById(R.id.maidongxidetupian);

text_kuan.setText("實付款: ¥"+countPriceBean.getPrice());

text_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

OkHttp3Util.doGet("https://www.zhaoapi.cn/product/createOrder?uid=2845&price=" + countPriceBean.getPrice(), new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();

CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Main2Activity.this, string,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);

// intent.putExtra("toux",chachebean.getData().get(0).getList().g)
// intent.putExtra("order",countPriceBean);

startActivity(intent);
}
});
}
});
}
});



}

public void huiqu2(View view) {
AlertDialog.Builder ab=new AlertDialog.Builder(Main2Activity.this);
ab.setTitle("你在三思三思~~~~");

ab.setPositiveButton("去意已決", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
ab.setNegativeButton("再看看",null);
ab.show();

}

public void dingdanzhongxin(View view) {
Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
startActivity(intent);
}

}

//佈局示例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:id="@+id/ding"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="26dp"
            android:onClick="huiqu2"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="確認訂單"
            android:textSize="24dp"
            android:layout_marginLeft="220dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="訂單中心"
            android:layout_marginLeft="180dp"
            android:onClick="dingdanzhongxin"
            />

    </LinearLayout>
    <TextView
        android:layout_below="@+id/ding"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"
        android:id="@+id/xian"
        />



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/tupian"

        >
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/maidongxidetupian"

            />
    </LinearLayout>

    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_kuan"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="實付款:"
            android:layout_marginLeft="120dp"
            android:layout_marginTop="13dp"
            android:textColor="#ff0000"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/text_order"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="立即下單"
            android:gravity="center"
            android:textColor="#fff"
            android:background="#ff0000"
            android:layout_height="match_parent" />

    </LinearLayout>
</RelativeLayout>

//到我的訂單界面

/**
* 我的訂單頁面
*/
public class Main3Activity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private List<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
tabLayout = (TabLayout) findViewById(R.id.tab2);
viewPager = (ViewPager) findViewById(R.id.view_pager2);

tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(GRAVITY_FILL);

list = new ArrayList<>();

list.add("全部");
list.add("待付款");
list.add("待收貨");
list.add("已完成");
list.add("已取消");

viewPager.setOffscreenPageLimit(list.size());
//1.給viewPager設置適配器
/**
* 管理者對象有幾種????
* getSupportFragmentManager()...activity管理他身上的fragment的時候使用
* getChildFragmentManager()...fragment嵌套的時候,,,管理孩子需要使用這個管理者
* getFragmentManager()....孩子裏面還有fragment的話就使用這個
*/
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {

//2.重寫這個方法getPageTitle,,,得到當前頁面的標題
@Override
public CharSequence getPageTitle(int position) {

return list.get(position);
}

@Override
public Fragment getItem(int position) {
NewsFragment1 newsFragment = new NewsFragment1();

//應該要做的是傳值,,,,去fragment裏面獲取,,,獲取到值之後,,,在進行url路徑的拼接

Bundle bundle = new Bundle();
bundle.putString("name",list.get(position));
newsFragment.setArguments(bundle);

return newsFragment;
}

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

//3.將tabLayout和viewPager關聯起來
tabLayout.setupWithViewPager(viewPager);

}

}

//佈局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main3Activity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textSize="24dp"
        android:textColor="#000"
        android:text="我的訂單"
        android:layout_gravity="center"
        ></TextView>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"
        />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab2"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorhong"


        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="@color/colorhei"
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tab">


    </android.support.v4.view.ViewPager>

</LinearLayout>

//我的訂單中的數據

public class NewsFragment1 extends Fragment{

private List<Dingbean.DataBean> list=new ArrayList<>();
List<Dingbean.DataBean> list1=new ArrayList<>();
private Base base;
private ListView lv;
int page=3;

Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1) {
Log.d("cccc", "進來了");
if ("全部".equals(name)) {
//Log.d("---",name);
list.clear();

list.addAll(dingbean.getData());
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
}


} else if ("待付款".equals(name)) {
Log.d("lllll", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 0) {
list.add(list1.get(i));
}

}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
}

} else {
getshuju();
}

} else if ("待收貨".equals(name)) {
Log.d("lllll", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 0) {
list.add(list1.get(i));
}

}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
}

} else {
getshuju();
}

} else if ("已完成".equals(name)) {
Log.d("----", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 2) {
list.add(list1.get(i));
}

}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
}

} else {
getshuju();
}

} else if ("已取消".equals(name)) {
Log.d("----", name);
if (list1.size() > 0) {
list.clear();
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).getStatus() == 2) {
list.add(list1.get(i));
}

}
if (list.size() > 0) {
setaba();
} else {
Toast.makeText(getActivity(), "沒有此類型的商品訂單哦", Toast.LENGTH_SHORT).show();
}

} else {
getshuju();
}

}


}else if (msg.what==2){

getshuju();
/*List<Dingbean.DataBean> date= (List<Dingbean.DataBean>) msg.obj;
Log.d("yyyyy", date.toString());
list.addAll(date);*/
setaba();


}
}
};
private String name;
private Dingbean dingbean;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ding, container, false);

lv = view.findViewById(R.id.lvvv);
return view;


}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);


name = getArguments().getString("name", "");
Log.d("nnnn", name);
if (name.equals("全部")){

getshuju();
}else if (name.equals("待付款")){
Log.d("vvvv","進來了");
handler.sendEmptyMessage(1);
}else if (name.equals("待收貨")){
handler.sendEmptyMessage(1);
}else if (name.equals("已完成")){
handler.sendEmptyMessage(1);
}else if (name.equals("已取消")){
handler.sendEmptyMessage(1);
}

}

private void getshuju() {
page++;
OkHttp3Util.doGet("https://www.zhaoapi.cn/product/getOrders?uid=71&page=6", new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

final String string = response.body().string();
if (response.isSuccessful()){

CommonUtils.runOnUIThread(new Runnable() {
@Override
public void run() {
Gson gson=new Gson();
dingbean = gson.fromJson(string, Dingbean.class);
list1.clear();
list1.addAll(dingbean.getData());
handler.sendEmptyMessage(1);

}
});

}

}
});


}

private void setaba() {

if (base==null){
base = new Base(getActivity(),list,handler);
lv.setAdapter(base);

}else
{
base.notifyDataSetChanged();
}

}

///////給一個listview就可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章