前言
又是隔了好久没写博客了,不知道是太急着学习还是怎么回事,总感觉写博客会占用蛮多时间的,可能还是我太懒了吧,不写博客的话中间学了好多的东西也没有巩固,最近感觉个人学习方法太单一,之前一个人完整开发完一个项目以后觉得什么都会了,继续学下去才发现自己这么点东西真的只算是个皮毛,最近android学完了又在学web后台,陷入一种迷茫中,心里想学精通一样,又担心移动市场目前饱和的现状,一边学着又一边地心不在焉,不过既然学了就好好学吧,总没坏处啊,至少这段时间的看视频加看书让我了解了蛮多东西,把设计模式和一些设计模式中的原则用到开发中,是在感觉很奇妙,而且就算工作的时候是要精通一样,如果以后创业什么的现在的这些也不会算白学呀,总之还是一句话,路漫漫其修远兮,还是要静下心来,厚积薄发。这个项目的代码太多了,之前也没有什么架构,自我现在感觉代码写的不是很好。后面的代码我直接全贴上来吧,免得回头忙起来又不管不顾的了。项目我会另行打包作为资源发出来的,大家应该可以在我博客的资源里找到。
源代码
adapter包下类:
package com.tmall.adapter;
import java.util.List;
import org.apache.http.Header;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tmall.R;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;
import com.tmall.bean.Car;
import com.tmall.net.CommonHttpHandler;
import com.tmall.net.HttpUtil;
import com.tmall.view.base.BaseApplication;
public class CarAdapter extends MyBaseAdapter {
public CarAdapter(List<Car> list) {
super(list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.car_list_item, null);
holder = new ViewHolder();
holder.iv = (ImageView) convertView.findViewById(R.id.car_item_iv);
holder.tvTitl = (TextView) convertView
.findViewById(R.id.car_item_tv_title);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.car_item_tv_price);
holder.tvCount = (TextView) convertView
.findViewById(R.id.car_item_tv_count);
holder.btnDelete = (Button) convertView
.findViewById(R.id.car_item_btn_delete);
holder.btnAdd = (Button) convertView
.findViewById(R.id.car_item_btn_add);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitl.setText(list.get(position).getTitle());
holder.tvPrice.setText("¥"
+ String.valueOf(list.get(position).getPrice()));
holder.tvCount.setText(list.get(position).getProCount() + "");
// picasso框架加载图片
Picasso.with(BaseApplication.context)
.load(list.get(position).getProUrl())
.config(Bitmap.Config.RGB_565).fit().into(holder.iv);
CarBtnClickListener listener = new CarBtnClickListener(position, holder);
holder.btnAdd.setOnClickListener(listener);
holder.btnDelete.setOnClickListener(listener);
return convertView;
}
public void addObserver() {
}
public void setData(List<Car> list) {
this.list = list;
notifyDataSetChanged();// 刷新界面
}
public int serTotalPrice() {
int totalprice = 0;
for (int i = 0; i < list.size(); i++) {
totalprice += list.get(i).getPrice() * list.get(i).getProCount();
}
return totalprice;
}
class ViewHolder {
ImageView iv;
TextView tvTitl;
TextView tvPrice;
TextView tvCount;
Button btnDelete;
Button btnAdd;
}
class CarBtnClickListener implements OnClickListener {
private int position;
private ViewHolder holder;
private int count;
public CarBtnClickListener(int position, ViewHolder holder) {
this.position = position;
this.holder = holder;
}
@Override
public void onClick(View v) {
int id = v.getId();
String countStr = holder.tvCount.getText().toString();
count = Integer.parseInt(countStr);
switch (id) {
case R.id.car_item_btn_add:
count += 1;
break;
case R.id.car_item_btn_delete:
count -= 1;
break;
default:
break;
}
// 无论增删,都会触发htttp请求
httpUpdate();
}
private void httpUpdate() {
String url = HttpUtil.BASE_URL + "update";
RequestParams params = new RequestParams();
params.put("request", "updatecar");
params.put("count", count);
params.put("proid", list.get(position).getProId());
params.put("username", list.get(position).getUserName());
params.put("id", list.get(position).getId());
HttpUtil.post(url, params, new CommonHttpHandler() {
@Override
public void success(int arg0, Header[] arg1, byte[] bytes) {
String result = new String(bytes);
if ("ok".equals(result)) {
// 更改ui并提醒界面更新数据
if (count > 0) {
holder.tvCount.setText(count + "");
list.get(position).
setProCount(count);
} else if (count == 0) {
list.remove(position);
setData(list);
}
}
}
});
}
}
}
package com.tmall.adapter;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tmall.R;
import com.tmall.bean.CenterItem;
import com.tmall.view.base.BaseApplication;
public class CenterListAdapter extends MyBaseAdapter {
public CenterListAdapter(List<CenterItem> list) {
super(list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.center_list_item, null);
holder = new ViewHolder();
holder.ivLeft = (ImageView) convertView
.findViewById(R.id.center_item_iv_left);
holder.tv = (TextView) convertView
.findViewById(R.id.center_item_tv);
holder.ivRight = (ImageView) convertView
.findViewById(R.id.center_item_iv_right);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CenterItem item = list.get(position);
holder.ivLeft.setBackgroundResource(item.getIvLeftId());
holder.tv.setText(item.getText());
holder.ivRight.setBackgroundResource(item.getIvRightId());
return convertView;
}
class ViewHolder {
ImageView ivLeft;
TextView tv;
ImageView ivRight;
}
}
package com.tmall.adapter;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.tmall.R;
import com.tmall.bean.Comments;
import com.tmall.view.base.BaseApplication;
public class CommentAdapter extends MyBaseAdapter {
public CommentAdapter(List<Comments> list) {
super(list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.comment_list_item, null);
holder = new ViewHolder();
holder.tvUsername = (TextView) convertView
.findViewById(R.id.comment_tv_username);
holder.tvComment = (TextView) convertView
.findViewById(R.id.comment_tv_words);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvUsername.setText(list.get(position).getUsername());
holder.tvComment.setText(list.get(position).getComment());
return convertView;
}
class ViewHolder {
TextView tvUsername;
TextView tvComment;
}
public void setData(List<Comments> list) {
this.list = list;
}
}
package com.tmall.adapter;
import java.util.List;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.tmall.R;
import com.squareup.picasso.Picasso;
import com.tmall.bean.DetailImg;
import com.tmall.view.base.BaseApplication;
public class DetailListAdapter extends MyBaseAdapter {
public DetailListAdapter(List<DetailImg> list) {
super(list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.detail_list_item, null);
holder = new ViewHolder();
holder.iv = (ImageView) convertView
.findViewById(R.id.detail_item_iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// picasso加载图片
Picasso.with(BaseApplication.context).load(list.get(position).getUrl())// http;//192.165.1:8080/Tmall/image/1.jpg
.config(Bitmap.Config.RGB_565).fit().into(holder.iv);
return convertView;
}
public void setData(List<DetailImg> list) {
this.list = list;
notifyDataSetChanged();// 刷新界面
}
class ViewHolder {
ImageView iv;
}
}
package com.tmall.adapter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tmall.R;
import com.squareup.picasso.Picasso;
import com.tmall.view.base.BaseApplication;
public class GridAdapter extends BaseAdapter {
private int[] ids;
private String datas[];
public GridAdapter(int[] ids, String[] datas, Resources res) {
this.ids = ids;
this.datas = datas;
}
@Override
public int getCount() {
return ids.length;
}
@Override
public Object getItem(int position) {
return ids[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.brand_grid_item, null);
holder = new ViewHolder();
holder.iv = (ImageView) convertView.findViewById(R.id.grid_item_iv);
holder.tv = (TextView) convertView.findViewById(R.id.grid_item_tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(datas[position]);
Picasso.with(BaseApplication.context).load(ids[position])
.config(Bitmap.Config.RGB_565).fit().into(holder.iv);
return convertView;
}
class ViewHolder {
ImageView iv;
TextView tv;
}
}
package com.tmall.adapter;
import java.util.List;
import com.example.tmall.R;
import com.tmall.bean.HomeItem;
import com.tmall.view.base.BaseApplication;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class HomeListAdapter extends MyBaseAdapter {
public HomeListAdapter(List<HomeItem> list) {
super(list);
}
class ViewHolder {
ImageView ivSign;
TextView tvSign;
ImageView ivSkip;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.home_list_item, null);
holder = new ViewHolder();
holder.ivSign = (ImageView) convertView
.findViewById(R.id.list_iv_sign);
holder.tvSign = (TextView) convertView
.findViewById(R.id.list_tv_sign);
holder.ivSkip = (ImageView) convertView
.findViewById(R.id.list_iv_skip);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
System.out.println("-----");
HomeItem bean = list.get(position);
holder.tvSign.setText(bean.getTvSign());
holder.ivSign.setImageResource(bean.getIvSignId());
holder.ivSkip.setImageResource(bean.getIvSkipId());
return convertView;
}
}
package com.tmall.adapter;
import java.util.List;
import android.widget.BaseAdapter;
public abstract class MyBaseAdapter extends BaseAdapter {
protected List list;
public MyBaseAdapter(List<T> list) {
super();
this.list = list;
}
@Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
package com.tmall.adapter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.tmall.utils.CommonUtils;
import com.tmall.view.base.BaseApplication;
public class MyPageAdapter extends PagerAdapter {
private int[] datas;
private Resources res;
public MyPageAdapter(int[] datas, Resources res) {
this.datas = datas;
this.res = res;
}
@Override
public int getCount() {
return datas.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int i = datas[position];
ImageView imageView = new ImageView(BaseApplication.context);
// 內存优化防止内存溢出的问题发生
Bitmap bitmap = CommonUtils.decordBitmap(BaseApplication.context, i);
Drawable drawable = new BitmapDrawable(res, bitmap);
imageView.setBackground(drawable);
container.addView(imageView);
return imageView;
}
}
package com.tmall.adapter;
import java.util.List;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tmall.R;
import com.squareup.picasso.Picasso;
import com.tmall.bean.Order;
import com.tmall.bean.OrderItem;
import com.tmall.view.base.BaseApplication;
public class OrderAdapter extends BaseAdapter {
private List<OrderItem> list;
public OrderAdapter(List<OrderItem> list) {
this.list = list;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.order_list_item, null);
holder = new ViewHolder();
holder.iv = (ImageView) convertView
.findViewById(R.id.order_item_iv);
holder.tvTitle = (TextView) convertView
.findViewById(R.id.order_item_tv_title);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.order_item_tv_price);
holder.tvCount = (TextView) convertView
.findViewById(R.id.order_item_tv_count);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Order order = Order.getInstance();
holder.tvTitle.setText(list.get(position).getTitle());
holder.tvCount.setText("x" + list.get(position).getProCount() + "");
holder.tvPrice.setText("¥" + list.get(position).getPrice() + "");
Picasso.with(BaseApplication.context)
.load(list.get(position).getProUrl())
.config(Bitmap.Config.RGB_565).fit().into(holder.iv);
return convertView;
}
class ViewHolder {
ImageView iv;
TextView tvTitle;
TextView tvPrice;
TextView tvCount;
}
@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;
}
}
package com.tmall.adapter;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.tmall.R;
import com.squareup.picasso.Picasso;
import com.tmall.bean.Product;
import com.tmall.view.base.BaseApplication;
public class ProductListAdapter extends BaseAdapter {
private Activity activity;
private List<Product> proList;
public ProductListAdapter(Activity activity, List<Product> proList) {
this.activity = activity;
this.proList = proList;
}
@Override
public int getCount() {
if (proList != null) {
return proList.size();
}
return 0;
}
@Override
public Object getItem(int position) {
return proList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("进入适配器");
ViewHolder holder = null;
// 当第一次访问时候findview加载
if (convertView == null) {
convertView = View.inflate(BaseApplication.context,
R.layout.product_list_item, null);
holder = new ViewHolder();
holder.tvSaleText = (TextView) convertView
.findViewById(R.id.sale_text);
holder.tvTitle = (TextView) convertView
.findViewById(R.id.pro_item_title);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.pro_item_price);
holder.tvSales = (TextView) convertView
.findViewById(R.id.pro_item_sales);
holder.tvCarriageStyle = (TextView) convertView
.findViewById(R.id.pro_item_carriagestyle);
holder.ivProduct = (ImageView) convertView
.findViewById(R.id.pro_item_iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Product product = proList.get(position);
// 设置数据到ui上面
holder.tvTitle.setText(product.getTitle());iner.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(BaseApplication.context);
// 优化性能和内存
Picasso.with(BaseApplication.context).load(list.get(position).getUrl())
.config(Bitmap.Config.RGB_565).fit().into(imageView);
container.addView(imageView);
return imageView;
}
public void setData(List<BigPic> list) {
this.list = list;
notifyDataSetChanged();
}
}
这里就不一一解释了,这些adapter类有哪些问题呢,首先由于部分持有activity的引用因此如果做一些耗时操作,例如新开子线程访问网络请求然后关闭了程序 ,由于activity仍被持有引用因此会导致内存泄
———-漏,这里可以用弱
引用加
以判断解决此问题,还有一些内部类最好写成静态不然也会持有外部类的n, Object object) {
co
n
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(BaseApplication.context);
// 优化性能和内存
Picasso.with(BaseApplication.context).load(list.get(position).getUrl())
.config(Bitmap.Config.RGB_565).fit().into(imageView);
container.addView(imageView);
return imageView;
}
public void setData(List<BigPic> list) {
this.list = list;
notifyDataSetChanged();
}
}
这里就不一一解释了,这些adapter类有哪些问题呢,首先由于部分持有activity的引用因此如果做一些耗时操作,例如新开子线程访问网络请求然后关闭了程序 ,由于activity仍被持有引用因此会导致内存泄
———-漏,这里可以用弱
引用加
以判断解决此问题,还有一些内部类最好写成静态不然也会持有外部类的n, Object object) {
co
ea
–
下面是 #
tainer.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(BaseApplication.context);
// 优化性能和内存
Picasso.with(BaseApplication.context).load(list.get(position).getUrl())
.config(Bitmap.Config.RGB_565).fit().into(imageView);
container.addView(imageView);
return imageView;
}
public void setData(List<BigPic> list) {
this.list = list;
notifyDataSetChanged();
}
}
这里就不一一解释了,这些adapter类有哪些问题呢,首先由于部分持有activity的引用因此如果做一些耗时操作,例如新开子线程访问网络请求然后关闭了程序 ,由于activity仍被持有引用因此会导致内存泄漏,这里可以用弱引用加以判断解决此问题,还有一些内部类最好写成静态不然也会持有外部类的引用从而导致内存泄漏。