Android ListViewDemo
生活随笔
收集整理的這篇文章主要介紹了
Android ListViewDemo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
最近在做電商APP,實現購物車功能,按照要求需點擊加減改變數量,計算每條數據的小計,及計算全部數據總價。之前都是使用SQLite保存數據,實現起來邏輯處理很麻煩。今天利用下班時間研究了下,寫了個Demo實現了想要的功能。下面貼出主要代碼,請大家批評指導。
百度網盤分享地址:http://pan.baidu.com/s/1gdw6gz5
MainActivity.java
package?com.srain.activity;import?java.util.ArrayList; import?java.util.HashMap; import?java.util.List; import?java.util.Map;import?android.app.Activity; import?android.content.Intent; import?android.os.Bundle; import?android.util.Log; import?android.view.View; import?android.view.View.OnClickListener; import?android.widget.AdapterView; import?android.widget.ListView; import?android.widget.TextView;import?com.srain.listviewdemo.R; import?com.srain.adapter.ListViewAdapter;public?class?MainActivity?extends?Activity?{private?List<Map<String,?Object>>?mData;private?ListView?listview;private?ListViewAdapter?adapter;private?TextView?tvAmount;private?double?amount?=?0;@Overridepublic?void?onCreate(Bundle?savedInstanceState)?{super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private?void?initView()?{mData?=?getData();tvAmount?=?(TextView)?findViewById(R.id.tv_amount);listview?=?(ListView)?findViewById(R.id.listleft);adapter?=?new?ListViewAdapter(this,?mLeftListOnItemClick,?mData);listview.setAdapter(adapter);listview.setOnItemClickListener(mLeftListOnItemClick);tvAmount.setText(""?+?amount);TextView?tvSub?=?(TextView)?findViewById(R.id.tv_sub);tvSub.setOnClickListener(new?OnClickListener()?{@Overridepublic?void?onClick(View?arg0)?{for?(Map<String,?Object>?map?:?mData)?{Log.e("count",?map.get("count").toString());}}});//?setlistViewLeft.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//?setlistViewLeft.setItemChecked(0,?true);//?setlistViewLeft.setSelection(0);}private?List<Map<String,?Object>>?getData()?{amount?=?0;List<Map<String,?Object>>?list?=?new?ArrayList<Map<String,?Object>>();Map<String,?Object>?map?=?new?HashMap<String,?Object>();map.put("price",?12);map.put("count",?2);list.add(map);map?=?new?HashMap<String,?Object>();map.put("price",?150);map.put("count",?3);list.add(map);map?=?new?HashMap<String,?Object>();map.put("price",?120);map.put("count",?8);list.add(map);map?=?new?HashMap<String,?Object>();map.put("price",?112);map.put("count",?6);list.add(map);map?=?new?HashMap<String,?Object>();map.put("price",?102);map.put("count",?6);list.add(map);//?for?(Map<String,?Object>?pram?:?list)?{//?int?count?=?Integer.valueOf(pram.get("count").toString());//?double?price?=?Double.valueOf(pram.get("price").toString());//?double?sub?=?count?*?price;//?amount?=?amount?+?sub;//?}for?(int?i?=?0;?i?<?list.size();?i++)?{Map<String,?Object>?pram?=?list.get(i);int?count?=?Integer.valueOf(pram.get("count").toString());double?price?=?Double.valueOf(pram.get("price").toString());double?sub?=?count?*?price;amount?=?amount?+?sub;Log.e("amount",?"amount"?+?amount?+?"sub"?+?sub?+?"i"?+?i);}return?list;}public?class?MyListener?implements?View.OnClickListener?{private?TextView?tvcount;private?TextView?tvsub;private?int?count;private?double?price;private?int?position;public?MyListener()?{}public?MyListener(TextView?tvcount,?TextView?tvsub,?int?count,double?price,?int?position)?{this.tvcount?=?tvcount;this.tvsub?=?tvsub;this.count?=?count;this.price?=?price;this.position?=?position;}@Overridepublic?void?onClick(View?view)?{switch?(view.getId())?{case?R.id.tv_add:count++;double?subtotle?=?price?*?count;tvcount.setText(""?+?count);tvsub.setText(""?+?subtotle);amount?+=?price;tvAmount.setText(""?+?amount);Map<String,?Object>?pram?=?mData.get(position);pram.put("price",?price);pram.put("count",?count);break;case?R.id.tv_del:break;default:break;}}}//?OnClickListener?bPop?=?new?OnClickListener()?{//?@Override//?public?void?onClick(View?v)?{//?for(int?i=0;i<list.getCount();i++){//?if(MyAdapter.isSelected.get(i)){//?ViewHolder?vHollder?=?(ViewHolder)?list.getChildAt(i).getTag();//?Log.i(TAG,?"--onClick?--"+vHollder.title.getText());//?}//?}//?}//?};AdapterView.OnItemClickListener?mLeftListOnItemClick?=?new?AdapterView.OnItemClickListener()?{public?void?onItemClick(AdapterView<?>?parent,?View?view,?int?position,long?id)?{Map<String,?Object>?map?=?mData.get(position);TextView?tvadd?=?(TextView)?view.findViewById(R.id.tv_add);TextView?tvcount?=?(TextView)?view.findViewById(R.id.tv_count);TextView?tvsubtotal?=?(TextView)?view.findViewById(R.id.tv_subtotal);int?count?=?Integer.valueOf(map.get("count").toString());double?price?=?Double.valueOf(map.get("price").toString());double?subtotal?=?Double.valueOf(tvsubtotal.getText().toString());tvadd.setOnClickListener(new?MyListener(tvcount,?tvsubtotal,?count,price,?position));Intent?intent?=?new?Intent();intent.setClass(MainActivity.this,?DetailActivity.class);startActivity(intent);//?adapter.setSelectItem(position);//?adapter.notifyDataSetInvalidated();//?adapter.notifyDataSetChanged();}};}ListViewAdapter.java
package?com.srain.adapter;import?java.util.List; import?java.util.Map;import?android.content.Context; import?android.graphics.Color; import?android.view.LayoutInflater; import?android.view.View; import?android.view.ViewGroup; import?android.widget.AdapterView.OnItemClickListener; import?android.widget.BaseAdapter; import?android.widget.TextView;import?com.srain.listviewdemo.R;public?class?ListViewAdapter?extends?BaseAdapter?{private?LayoutInflater?mInflater;private?List<Map<String,?Object>>?mData;private?OnItemClickListener?listener;public?ListViewAdapter(Context?context,?OnItemClickListener?listener,List<Map<String,?Object>>?mData)?{this.mData?=?mData;this.mInflater?=?LayoutInflater.from(context);this.listener?=?listener;}public?int?getCount()?{return?mData.size();}public?Object?getItem(int?arg0)?{return?mData.get(arg0);}public?long?getItemId(int?arg0)?{return?arg0;}public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{Map<String,?Object>?map?=?mData.get(position);ViewHolder?holder?=?null;if?(convertView?==?null)?{holder?=?new?ViewHolder();convertView?=?mInflater.inflate(R.layout.leftview,?null);holder.tvadd?=?(TextView)?convertView.findViewById(R.id.tv_add);holder.tvdel?=?(TextView)?convertView.findViewById(R.id.tv_del);holder.tvConut?=?(TextView)?convertView.findViewById(R.id.tv_count);holder.tvSubtotal?=?(TextView)?convertView.findViewById(R.id.tv_subtotal);convertView.setTag(holder);}?else?{holder?=?(ViewHolder)?convertView.getTag();}int?count?=?Integer.valueOf(map.get("count").toString());double?price?=?Double.valueOf(map.get("price").toString());holder.tvConut.setText(count?+?"");holder.tvSubtotal.setText(price?*?count?+?"");if?(position?==?selectItem)?{convertView.setBackgroundColor(Color.RED);}?else?{convertView.setBackgroundColor(Color.TRANSPARENT);}return?convertView;}public?void?setSelectItem(int?selectItem)?{this.selectItem?=?selectItem;}private?int?selectItem?=?0;public?final?class?ViewHolder?{public?TextView?tvadd;public?TextView?tvdel;public?TextView?tvConut;public?TextView?tvSubtotal;} }轉載于:https://my.oschina.net/u/2320057/blog/392437
總結
以上是生活随笔為你收集整理的Android ListViewDemo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌技术主管:当我们选择工作时,什么最重
- 下一篇: xxx while the manage