首页 » 技术分享 » 京东商城选择地址信息

京东商城选择地址信息

 

京东商城显示有货无货信息

前几天要和京东做接口,其中用到客户选择完地址后,根据选择地址来判断是否有
货,以下是截图

点击送至xxx
选择省份

选择区县

代码块

用到的技术:

  1. activity间值的传递
  2. listview列表刷新
  3. 异步请求
package com.cric.intelem.activity;


import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cric.intelem.ApplicationContext;
import com.cric.intelem.MyHttpClient;
import com.cric.intelem.R;
import com.cric.intelem.adapter.AddressAdapter;
import com.cric.intelem.bean.ExGroup;
import com.cric.intelem.bean.IntelemHost;
import com.cric.intelem.bean.User;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;


public class SelectAddressActivity extends Activity {

    private int page =1;
    private int ProvinceID=0;
    private String ProvinceName="";
    private int CityID=0;
    private String CityName="";
    private int CountyID=0;
    private String CountyName="";
    private Context context;
    private boolean lock = false;
    ListView listview;
    ArrayList<ExGroup> list = new ArrayList<ExGroup>();
    ApplicationContext app;
    AddressAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        app = (ApplicationContext) getApplication();
        setContentView(R.layout.select_address_layout);
        ((TextView) findViewById(R.id.layout_header_title_id)).setText("配送至");
        listview = (ListView) findViewById(R.id.layout_select_address_id);
        adapter = new AddressAdapter(context, list);
        list = initlist(page);
        listview.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listview.setOnItemClickListener(oilistener);
        ((ImageView)findViewById(R.id.layout_header_btn_right_image_id))
        .setVisibility(View.INVISIBLE);
        ((ImageView)findViewById(R.id.layout_header_btn_left_image_id))
        .setImageResource(R.drawable.btn_back);
        LinearLayout layoutleft =  (LinearLayout) findViewById(R.id.layout_header_btn_left_id);
        layoutleft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
/**
 * 获取省份列表
 * @param page
 * @return
 */
    private ArrayList<ExGroup> initlist(int page){
        if(page>1){
            list.clear();
        }

        String requestURL = IntelemHost.JDProvince;
        String UserID = app.getUid();
        User user = app.getLoginInfo();
        String no = user.getCookie();
        RequestParams params = new RequestParams();

        params.put("UserID", UserID);
        params.put("CaptchaNo", no);
        MyHttpClient.get(requestURL, params, new AsyncHttpResponseHandler(){    


            @Override
            public void onSuccess(String content) {
                //System.out.println(content);
                JSONArray array;
                try {
                    JSONObject  jsonObject = new JSONObject(content);
                    array =  jsonObject.getJSONArray("getprovince");
                    for(int i=0;i<array.length();i++){
                        JSONObject temp = (JSONObject) array.get(i); 
                        ExGroup gz = new ExGroup();
                        String provincename = temp.getString("ProvinceName");
                        String provinceid = temp.getString("ProvinceID");
                        int id = Integer.parseInt(provinceid);
                        gz.setName(provincename);
                        gz.setId(id);
                        gz.setHasNew(false);
                        list.add(gz);

                    }

                    if(null!=adapter){
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }

        }

            @Override
            public void onStart() {


            }


            @Override
            public void onFailure(Throwable error, String content) {

            }



        });
        return list;

    }

    /**
     * 获取城市列表
     * @param page
     * @return
     */
    private ArrayList<ExGroup> initcitylist(int page,int pid){
        if(page>1){
            list.clear();
        }

        String requestURL = IntelemHost.JDCity;
        String UserID = app.getUid();
        User user = app.getLoginInfo();
        String no = user.getCookie();
        RequestParams params = new RequestParams();

        params.put("UserID", UserID);
        params.put("CaptchaNo", no);
        params.put("Province", pid+"");
        MyHttpClient.get(requestURL, params, new AsyncHttpResponseHandler(){    


            @Override
            public void onSuccess(String content) {
                lock = false;
                JSONArray array;
                try {
                    JSONObject  jsonObject = new JSONObject(content);
                    array =  jsonObject.getJSONArray("getcity");
                    for(int i=0;i<array.length();i++){
                        JSONObject temp = (JSONObject) array.get(i); 
                        ExGroup gz = new ExGroup();
                        String provincename = temp.getString("CityName");
                        String provinceid = temp.getString("CityID");
                        int id = Integer.parseInt(provinceid);
                        gz.setName(provincename);
                        gz.setId(id);
                        gz.setHasNew(false);
                        list.add(gz);

                    }

                    if(null!=adapter){
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onStart() {
                lock = true;

            }


            @Override
            public void onFailure(Throwable error, String content) {

            }



        });
        return list;

    }

    /**
     * 获取区县列表
     * @param page
     * @return
     */
    private ArrayList<ExGroup> initcountylist(int page,int cid){
        if(page>1){
            list.clear();
        }

        String requestURL = IntelemHost.JDCounty;
        String UserID = app.getUid();
        User user = app.getLoginInfo();
        String no = user.getCookie();
        RequestParams params = new RequestParams();

        params.put("UserID", UserID);
        params.put("CaptchaNo", no);
        params.put("City", cid+"");
        MyHttpClient.get(requestURL, params, new AsyncHttpResponseHandler(){    


            @Override
            public void onSuccess(String content) {
                lock = false;
                JSONArray array;
                try {
                    JSONObject  jsonObject = new JSONObject(content);
                    array =  jsonObject.getJSONArray("getcounty");
                    for(int i=0;i<array.length();i++){
                        JSONObject temp = (JSONObject) array.get(i); 
                        ExGroup gz = new ExGroup();
                        String provincename = temp.getString("CountyName");
                        String provinceid = temp.getString("CountyID");
                        int id = Integer.parseInt(provinceid);
                        gz.setName(provincename);
                        gz.setId(id);
                        gz.setHasNew(false);
                        list.add(gz);

                    }

                    if(null!=adapter){
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onStart() {
                lock = true;

            }


            @Override
            public void onFailure(Throwable error, String content) {

            }



        });
        return list;

    }

    /**
     * 这里是点击条目是促发的事件
     * 第一次点击选择省份
     * 第二次点击选择城市
     * 第三次点击选择区县
     * 每次都获取列表前都清空数据
     * 第三次点击后将获取的值返回给前一个acivity并关闭页面
     */

    public OnItemClickListener oilistener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long id) {

            if((list.size()==0)||lock){
                return;
            }else{
                page++;
            }
            int realPosition=(int)id;
            ExGroup eg = list.get(realPosition);
            int pid = eg.getId();
            String pname = eg.getName();

            if(page==2){
                ProvinceID =pid;
                ProvinceName = pname;
                initcitylist(page,pid);
            }else if(page==3){
                CityID= pid;
                CityName= pname;
                initcountylist(page, pid);

            }else if(page==4){
                CountyID = pid;
                CountyName = pname;
                Intent intent = new Intent();
                intent.putExtra("ProvinceID", ProvinceID);
                intent.putExtra("ProvinceName", ProvinceName);
                intent.putExtra("CityID", CityID);
                intent.putExtra("CityName", CityName);
                intent.putExtra("CountyID", CountyID);
                intent.putExtra("CountyName", CountyName);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }

        }
    };

}

这里是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="300dp"
     <!--这里宽度为300dp可以让页面显示半边,居右对其-->
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="vertical"  >
<include layout="@layout/layout_header" />
    <LinearLayout
        android:id="@+id/dialog_layout"
        android:layout_below="@id/layout_header_id"
        android:layout_width="fill_parent"

        android:layout_height="wrap_content"
        android:background="#ffeeeef3"
        android:paddingBottom="5.0dip" 
        android:gravity="center_horizontal"
        android:orientation="vertical" > 

        <ListView 
       android:id="@+id/layout_select_address_id"
       android:layout_width="300dp"
       android:layout_height="wrap_content"
       android:cacheColorHint="@android:color/transparent"
       android:scrollbars="none"
       android:groupIndicator="@null"
       android:listSelector="@android:color/darker_gray"
       android:divider="@drawable/layout_pop_addresslistview_divider"/>


    </LinearLayout>

</RelativeLayout>

转载自原文链接, 如需删除请联系管理员。

原文链接:京东商城选择地址信息,转载请注明来源!

0