android收货地址整理


android 收货地址编写 :


AddressActivity:


import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.jydev.myn.R;
import com.jydev.myn.base.BaseActivity;
import com.jydev.myn.base.LocationApplication;
import com.jydev.myn.bean.AddressInfo;
import com.jydev.myn.bean.AsMessageEvent;
import com.jydev.myn.bean.AsOtherEvent;
import com.jydev.myn.db.AddressDB;
import com.jydev.myn.utils.ACache;
import com.nostra13.universalimageloader.utils.L;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by pc on 2016/5/16.
 */
public class AddressActivity extends BaseActivity {

    @Bind(R.id.city_back)
    ImageView cityBack;
    @Bind(R.id.txt_topbar)
    TextView txtTopbar;
    @Bind(R.id.iv_search)
    ImageView ivSearch;
    @Bind(R.id.ly_top_bar)
    RelativeLayout lyTopBar;
    @Bind(R.id.tv_add_address)
    TextView tvAddAddress;
    @Bind(R.id.tv_manager)
    TextView tvManager;
    @Bind(R.id.tv_common_address)
    TextView tvCommonAddress;

    private SwipeMenuListView listView;
    private List list;
    private AddressAdapter adapter;
    private int str_position = -1;
    private ACache mCache;
    private int address_id;
    private int alter_position;
    private List address = new ArrayList();
    private AddressDB addressDB;
    private int circle_position;

    @Override
    public int getLayoutId() {
        return R.layout.activity_address_list;
    }

    @Override
    public void doMainUI() {

        //初始化布局界面
        txtTopbar.setText("选择服务地址");
        ivSearch.setVisibility(View.GONE);

        //sqlite
        addressDB = AddressDB.getInstance(getBaseContext());
        address = addressDB.queryAddress();

        //常用地址是否可见
        if (address == null) {
            tvCommonAddress.setVisibility(View.INVISIBLE);
        }

        initView();
        mCache = ACache.get(this);

    }

    private void initView() {

        listView = ((SwipeMenuListView) findViewById(R.id.listView));
        //虚拟数据源
//        for (int i = 0; i < 30; i++) {
//            list.add("item" + i);
//        }

        adapter = new AddressAdapter();
        listView.setAdapter(adapter);
        //点击事件一样写
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
//                Toast.makeText(mContext, "你点击了" + list.get(position), Toast.LENGTH_SHORT).show();
                // TODO Auto-generated method stub
                //圆形图的处理
                mCache.put("item_circle_position", position + "");
                Intent intent = new Intent(AddressActivity.this, OrderActivity.class);
               //保存地址,展现到订单页面
                String click_address = address.get(position).getProvinces() + address.get(position).getStreet();

                LocationApplication.mCache.put("click_address",click_address);

                startActivity(intent);
                finish();


            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
//                Toast.makeText(mContext, "longClick" + list.get(position), Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        //加入侧滑显示的菜单
        //1.首先实例化SwipeMenuCreator对象
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(mContext);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        listView.setMenuCreator(creater);

        //2.菜单点击事件
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                final AddressInfo a = address.get(position);

                switch (index) {
                    case 0:
                        //删除的逻辑
                        if (addressDB.deleteAddress(a)) {
                            Toast.makeText(getBaseContext(), "删除成功", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getBaseContext(), "删除失败", Toast.LENGTH_LONG).show();
                        }

                        address.remove(position);
                        adapter.notifyDataSetChanged();
                        if (address.size() == 0) {
                            tvCommonAddress.setVisibility(View.INVISIBLE);
                        }

                        break;
                }
                return false;
            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }


    @OnClick({R.id.city_back, R.id.txt_topbar, R.id.iv_search, R.id.ly_top_bar, R.id.tv_add_address, R.id.tv_manager})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.city_back:
                finish();
                break;
            case R.id.txt_topbar:
                break;
            case R.id.iv_search:

                break;
            case R.id.ly_top_bar:
                break;

            case R.id.tv_manager:

                break;
            case R.id.tv_add_address:
                Intent intent = new Intent(AddressActivity.this, AlterAddressActivity.class);

                startActivity(intent);
                break;
        }
    }


    class AddressAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return address != null ? address.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.item_address, null);
                holder = new ViewHolder();
                holder.txt = (TextView) convertView.findViewById(R.id.txt);
                holder.iv_add_address = (ImageView) convertView.findViewById(R.id.iv_add_address);
                holder.iv_checked = (ImageView) convertView.findViewById(R.id.iv_checked);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txt.setText(address.get(position).getProvinces() + address.get(position).getStreet());


            //选择checked点击事件
//            final ViewHolder finalHolder = holder;
//            holder.iv_checked.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    if (address.get(position).getStatus()) {
                        holder.iv_checked.setChecked(true);
//                        address.get(position).setStatus(false);
//                        Resources resources = mContext.getResources();
//                        Drawable drawable = resources.getDrawable(R.mipmap.circle);
//                        finalHolder.iv_checked.setImageDrawable(drawable);
//
//                    } else {
                        holder.iv_checked.setChecked(false);
//                        address.get(position).setStatus(true);
//                        Resources resources = mContext.getResources();
//                        Drawable drawable = resources.getDrawable(R.mipmap.circle_yes);
//                        finalHolder.iv_checked.setImageDrawable(drawable);
//                    }
//                }
//            });

            //编写更改的点击事件
            holder.iv_add_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, AlterAddressActivity.class);
                    Bundle b = new Bundle();
                    b.putSerializable("address", address.get(position));
                    L.d("AddressActivity", address.get(position));
                    intent.putExtra("address_id", b);

                    startActivity(intent);
                }
            });


            //显示出来

            String str = mCache.getAsString("item_circle_position");
            if(str!=null){
                circle_position = Integer.parseInt(str);
            }

           if(circle_position==position){

               Resources resources = mContext.getResources();
               Drawable drawable = resources.getDrawable(R.mipmap.circle_yes);
               holder.iv_checked.setImageDrawable(drawable);
           }




//            if (address.get(position).getStatus()) {
//
//                Resources resources = mContext.getResources();
//                Drawable drawable = resources.getDrawable(R.mipmap.circle_yes);
//                holder.iv_checked.setImageDrawable(drawable);
//
//
//            } else {
//
//                Resources resources = mContext.getResources();
//                Drawable drawable = resources.getDrawable(R.mipmap.circle);
//                holder.iv_checked.setImageDrawable(drawable);
//            }

            return convertView;
        }

        public class ViewHolder {
            private TextView txt;
            private ImageView iv_checked;
            private ImageView iv_add_address;

        }

    }


    // This method will be called when a MessageEvent is posted
    @Subscribe
    public void onMessageEvent(AsMessageEvent event) {
        Toast.makeText(AddressActivity.this, event.message, Toast.LENGTH_SHORT).show();
    }

    //    // This method will be called when a SomeOtherEvent is posted
    @Subscribe
    public void handleSomethingElse(AsOtherEvent event) {
        Toast.makeText(AddressActivity.this, event.message, Toast.LENGTH_SHORT).show();
    }


}

AlterActivity


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jydev.myn.R;
import com.jydev.myn.base.BaseActivity;
import com.jydev.myn.base.LocationApplication;
import com.jydev.myn.bean.AddressInfo;
import com.jydev.myn.db.AddressDB;
import com.jydev.myn.utils.ACache;
import com.jydev.myn.utils.ToastUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by pc on 2016/5/17.
 */
public class AlterAddressActivity extends BaseActivity {
    @Bind(R.id.city_back)
    ImageView cityBack;
    @Bind(R.id.txt_topbar)
    TextView txtTopbar;
    @Bind(R.id.iv_search)
    ImageView ivSearch;
    @Bind(R.id.tv_manager)
    TextView tvManager;
    @Bind(R.id.ly_top_bar)
    RelativeLayout lyTopBar;
    @Bind(R.id.tv_location_city)
    TextView tvLocationCity;
    @Bind(R.id.tv_address_name)
    TextView tvAddressName;
    @Bind(R.id.et_address_big)
    EditText etAddressBig;
    @Bind(R.id.et_city_small)
    EditText etCitySmall;
    @Bind(R.id.tv_delete_address)
    TextView tvDeleteAddress;

    private String address_big;
    private String address_small;
//    private ACache mCache;
    private String address;
    private String address_id;
    private AddressInfo myAddress;
    private AddressInfo addressinfo;
    private AddressDB addressDB;

    @Override
    public int getLayoutId() {

        return R.layout.activity_alter_address;

    }

    @Override
    public void doMainUI() {

        //初始化布局界面
        txtTopbar.setText("修改地址");
        ivSearch.setVisibility(View.GONE);
        tvManager.setVisibility(View.VISIBLE);
        //默认不显示删除
        //sqlite
        myAddress = new AddressInfo();//插入数据
        addressDB = AddressDB.getInstance(mContext);//new 一个数据库对象

        //  获得当前的item位置list集合的对象,这个是修改传过来得数据
        Intent intent = getIntent();
        Bundle b =  intent.getBundleExtra("address_id");

        //获取首页显示的城市,并显示

        String choice_city =  LocationApplication.mCache.getAsString("city");
        tvLocationCity.setText(choice_city);


        if(b!=null){
            addressinfo = (AddressInfo) b.get("address");

            etAddressBig.setText(addressinfo.getProvinces());
            etCitySmall.setText(addressinfo.getStreet());
            //同时显示出删除
            tvDeleteAddress.setVisibility(View.VISIBLE);

            //再次保存,这里是避免id为null
            myAddress.setId(addressinfo.getId());
            myAddress.setProvinces(addressinfo.getProvinces());
            myAddress.setStreet(addressinfo.getStreet());
            myAddress.setStatus(addressinfo.getStatus());

        }




    }


    @OnClick({R.id.city_back, R.id.txt_topbar, R.id.iv_search, R.id.tv_manager, R.id.ly_top_bar, R.id.tv_location_city, R.id.tv_address_name, R.id.et_address_big, R.id.et_city_small, R.id.tv_delete_address})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.city_back:
                finish();
                break;
            case R.id.txt_topbar:
                break;
            case R.id.iv_search:
                break;
            case R.id.tv_manager:
                // 保存数据
                myAddress.setStreet(etCitySmall.getText().toString());
                myAddress.setProvinces(etAddressBig.getText().toString());
                //判断信息是否完整
                if (myAddress.getProvinces().length() < 1 || myAddress.getStreet().length() < 1
                        ) {
                    ToastUtil.showShort(mContext,"请完整填写个人信息");
                    return;
                }

                //存入数据库


                if (addressinfo != null) {
                    //修改
                    if (addressDB.updeteAddress(myAddress)) {
                        Toast.makeText(getBaseContext(), "修改收货地址成功", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getBaseContext(), "修改收货地址失败", Toast.LENGTH_LONG).show();
                    }
                } else {
                    //先添加时间插入
                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                    Date date = new Date();
                    String id = format.format(date);
                    myAddress.setId(id);

                    //插入
                    if (addressDB.insertAddress(myAddress)) {
                        Toast.makeText(getBaseContext(), "添加收货地址成功", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getBaseContext(), "添加收货地址失败", Toast.LENGTH_LONG).show();
                    }
                }

                Intent intent = new Intent(AlterAddressActivity.this, AddressActivity.class);
                startActivity(intent);
                finish();
                break;

            case R.id.ly_top_bar:
                break;
            case R.id.tv_location_city:
                break;
            case R.id.tv_address_name:
                break;
            case R.id.et_address_big:
                break;
            case R.id.et_city_small:
                break;
            case R.id.tv_delete_address:
                //删除的逻辑
                if (addressDB.deleteAddress(addressinfo)) {
                    Toast.makeText(getBaseContext(), "删除成功", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getBaseContext(), "删除失败", Toast.LENGTH_LONG).show();
                }

                Intent intent_delete = new Intent(mContext,AddressActivity.class);
                startActivity(intent_delete);
                finish();

                break;
        }
    }
}

上面两个类是收货地址的主要代码:


参考的demo:


https://yunpan.cn/cSFjJ9pixcFbt  访问密码 46d4

https://yunpan.cn/cSFjVBspvpJ8U  访问密码 872f

https://yunpan.cn/cSFjwHuuka5Ka  访问密码 d664



你可能感兴趣的:(Android)