本文實(shí)例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計(jì),安岳網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:安岳等地區(qū)。安岳做網(wǎng)站價(jià)格咨詢:13518219792
MainActivity的布局文件
條目的布局文件
CartAdapter適配器
public class CaetAdapter extends BaseAdapter { private Context context; private List> list; private HashMap pitchOnMap; public HashMap getPitchOnMap() { return pitchOnMap; } public void setPitchOnMap(HashMap pitchOnMap) { this.pitchOnMap = pitchOnMap; } public CaetAdapter(Context context, List > list) { this.context = context; this.list = list; pitchOnMap = new HashMap<>(); for (int i = 0; i < list.size(); i++) { pitchOnMap.put(list.get(i).get("id"), 0); } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { convertView = View.inflate(context, R.layout.item_layout, null); final CheckBox checkBox; ImageView icon; final TextView name, price, num, type, reduce, add; checkBox = convertView.findViewById(R.id.check_box); icon = convertView.findViewById(R.id.iv_adapter_list_pic); name = convertView.findViewById(R.id.tv_goods_name); price = convertView.findViewById(R.id.tv_goods_price); type = convertView.findViewById(R.id.tv_type_size); num = convertView.findViewById(R.id.tv_num); reduce = convertView.findViewById(R.id.tv_reduce); add = convertView.findViewById(R.id.tv_add); name.setText(list.get(position).get("name")); price.setText("¥ " + (Integer.valueOf(list.get(position).get("price"))) * (Integer.valueOf(list.get(position).get("count")))); type.setText(list.get(position).get("type")); num.setText(list.get(position).get("count")); if(pitchOnMap.get(list.get(position).get("id"))== 0){ checkBox.setChecked(false); }else{ checkBox.setChecked(true); } checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(checkBox.isChecked()){ pitchOnMap.put(list.get(position).get("id"),1); }else{ pitchOnMap.put(list.get(position).get("id"), 0); } mrefreshPriceInterface.refreshPrice(pitchOnMap); } }); //商品數(shù)量減 reduce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Integer.valueOf(list.get(position).get("count")) <= 1) { Toast.makeText(context, "數(shù)量不能再減啦,只能刪除!", Toast.LENGTH_SHORT).show(); } else { list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) - 1) + ""); notifyDataSetChanged(); } mrefreshPriceInterface.refreshPrice(pitchOnMap); } }); //商品數(shù)量加 add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.get(position).put("count", (Integer.valueOf(list.get(position).get("count")) + 1) + ""); notifyDataSetChanged(); mrefreshPriceInterface.refreshPrice(pitchOnMap); } }); return convertView; } /** * 創(chuàng)建接口 */ public interface RefreshPriceInterface { /** * 把價(jià)格展示到總價(jià)上 * @param pitchOnMap */ void refreshPrice(HashMap pitchOnMap); } /** * 定義一個(gè)接口對(duì)象 */ private RefreshPriceInterface mrefreshPriceInterface; /** * 向外部暴露一個(gè)方法 * 把價(jià)格展示到總價(jià)上 * @param refreshPriceInterface */ public void setRefreshPriceInterface(RefreshPriceInterface refreshPriceInterface) { mrefreshPriceInterface = refreshPriceInterface; } }
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CaetAdapter.RefreshPriceInterface{ private LinearLayout top_bar; private ListView listview; private CheckBox all_chekbox; private TextView price; private TextView delete; private TextView tv_go_to_pay; private ListgoodsList; private UserDao userDao; private List > listmap=new ArrayList<>(); private CaetAdapter adapter; private double totalPrice = 0.00; private int totalCount = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { top_bar = (LinearLayout) findViewById(R.id.top_bar); listview = (ListView) findViewById(R.id.listview); all_chekbox = (CheckBox) findViewById(R.id.all_chekbox); price = (TextView) findViewById(R.id.tv_total_price); delete = (TextView) findViewById(R.id.tv_delete); tv_go_to_pay = (TextView) findViewById(R.id.tv_go_to_pay); all_chekbox.setOnClickListener(this); delete.setOnClickListener(this); tv_go_to_pay.setOnClickListener(this); initDate(); adapter = new CaetAdapter(MainActivity.this, listmap); listview.setAdapter(adapter); adapter.setRefreshPriceInterface(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.all_chekbox: AllTheSelected(); break; case R.id.tv_delete: checkDelete(adapter.getPitchOnMap()); break; case R.id.tv_go_to_pay: if(totalCount<=0){ Toast.makeText(this,"請(qǐng)選擇要付款的商品~",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this,"付款成功",Toast.LENGTH_SHORT).show(); } break; } } /** * 數(shù)據(jù) */ private void initDate() { //創(chuàng)建集合 goodsList = new ArrayList<>(); //數(shù)據(jù)庫(kù) userDao = Myapplication.getInstances().getDaoSession().getUserDao(); userDao.deleteAll(); //數(shù)據(jù)源 for (int i = 0; i < 10; i++) { //向數(shù)據(jù)庫(kù)存放數(shù)據(jù) User user = new User((long) i, "購(gòu)物車?yán)锏牡? + (i + 1) + "件商品", (i + 20) + "碼", "10", "10"); userDao.insert(user); } //從數(shù)據(jù)庫(kù)中把數(shù)據(jù)放到集合中 goodsList=userDao.loadAll(); //把結(jié)合中的數(shù)據(jù)放到HashMap集合中 for(int i=0;i map=new HashMap<>(); map.put("id",goodsList.get(i).getId()+""); map.put("name",goodsList.get(i).getName()); map.put("type",(goodsList.get(i).getType())); map.put("price",goodsList.get(i).getPrice()+""); map.put("count",goodsList.get(i).getCount()+""); listmap.add(map); } } @Override public void refreshPrice(HashMap pitchOnMap) { priceControl(pitchOnMap); } /** * 控制價(jià)格展示總價(jià) */ private void priceControl(Map pitchOnMap){ totalCount = 0; totalPrice = 0.00; for(int i=0;i map){ List > waitDeleteList=new ArrayList<>(); Map waitDeleteMap =new HashMap<>(); for(int i=0;i map=adapter.getPitchOnMap(); boolean isCheck=false; boolean isUnCheck=false; Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); if(Integer.valueOf(entry.getValue().toString())==1){ isCheck=true; }else{ isUnCheck=true; } } if(isCheck==true&&isUnCheck==false){//已經(jīng)全選,做反選 for(int i=0;i
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。