明明很简单的事,鼓捣了俩小时,现在写一些中间遇到的卡壳,没过去,Android客户端未联网时,一直都是自己模拟实现界面,但是要是架上服务器就不一样了,废话不多说,直接上代码:
这个是activity中的代码
commondSecondBeans = new ArrayList();
CommondSecondBean[] commondArray = new CommondSecondBean[] {
new CommondSecondBean(
"http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690",
"笑谈花落", "一句话说过你看过的星爷的电影,我先来,人要是没有理想和咸鱼有什么区别", "1", "2",
"3", "4"),
new CommondSecondBean(
"http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690",
"笑谈花落", "一句话说过你看过的星爷的电影,我先来,人要是没有理想和咸鱼有什么区别", "1", "2",
"3", "4"),
new CommondSecondBean(
"http://s16.sinaimg.cn/orignal/89429f6dhb99b4903ebcf&690",
"笑谈花落", "一句话说过你看过的星爷的电影,我先来,人要是没有理想和咸鱼有什么区别", "1", "2",
"3", "4") };
Gson mygson = new Gson();
String ing = mygson.toJson(commondArray);
Log.e("ing", ing);
JSONArray ja = new JSONArray(ing);
for (int j = 0; j < ja.length(); j++) {
String murls = ((JSONObject) ja.get(j)).getString("UserImg");
Log.e("murls", murls);
String mnames = ((JSONObject) ja.get(j)).getString("UserName");
Log.e("mnames", mnames);
String mdetails = ((JSONObject) ja.get(j)).getString("UserDetail");
Log.e("mdetails", mdetails);
String mgoods = ((JSONObject) ja.get(j)).getString("Good");
Log.e("mgoods", mgoods);
String mpoors = ((JSONObject) ja.get(j)).getString("Poor");
Log.e("mpoors", mpoors);
String mhots = ((JSONObject) ja.get(j)).getString("Hot");
Log.e("mhots", mhots);
String mshares = ((JSONObject) ja.get(j)).getString("Share");
Log.e("mshares", mshares);
}
for (int i = 0; i < commondArray.length; i++) {
CommondSecondBean commondSecondBeans = new CommondSecondBean(null,
null, null, i + "", i + "", i + "", i + "");
String murl = commondArray[i].getUserImg();
String mname = commondArray[i].getUserName();
String mdetail = commondArray[i].getUserDetail();
String mgood = commondArray[i].getGood();
String mpoor = commondArray[i].getPoor();
String mhot = commondArray[i].getHot();
String mshare = commondArray[i].getShare();
/*
* 测试gson转化 Bitmap mbitmap = getHttpBitmap(murl); Map map = new HashMap(); map.put("m_url",
* mbitmap); map.put("m_name", mname); map.put("m_detail", mdetail);
* map.put("m_good", mgood); map.put("m_poor", mpoor);
* map.put("m_hot", mhot); map.put("m_share", mshare);
* commondList.add(map); Gson gson = new Gson(); 将map转化为json String
* gsonStr = gson.toJson(map);
*/
}
// 将list转化为json
// Gson gson1 = new Gson();
// String gsonStr = gson1.toJson(commondList);
// System.out.println(gsonStr);
Arrays.sort(commondArray);
commondSecondBeans = Arrays.asList(commondArray);
myAdapter = new MyAdapterSecond(MainSecondActivity.this,
commondSecondBeans);
personal_listview.setAdapter(myAdapter);
这个是commondbean中的代码:
public class CommondBean implements java.lang.Comparable {
private String UserImg;
private String UserName;
private String UserDetail;
private String Good;
private String Poor;
private String Hot;
private String Share;
public CommondBean(String userImg, String userName, String userDetail,
String good, String poor, String hot, String share) {
UserImg = userImg;
UserName = userName;
UserDetail = userDetail;
Good = good;
Poor = poor;
Hot = hot;
Share = share;
}
public String getUserImg() {
return UserImg;
}
public void setUserImg(String userImg) {
UserImg = userImg;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserDetail() {
return UserDetail;
}
public void setUserDetail(String userDetail) {
UserDetail = userDetail;
}
public String getGood() {
return Good;
}
public void setGood(String good) {
Good = good;
}
public String getPoor() {
return Poor;
}
public void setPoor(String poor) {
Poor = poor;
}
public String getHot() {
return Hot;
}
public void setHot(String hot) {
Hot = hot;
}
public String getShare() {
return Share;
}
public void setShare(String share) {
Share = share;
}
@Override
public int compareTo(Object another) {
// TODO Auto-generated method stub
return 0;
}
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.loulijun.bean.CommondBean;
import com.loulijun.logindemo.R;
import com.loulijun.utils.ListViewImageTaskUtil;
public class MyAdapter extends BaseAdapter {
private Context context;
private List
// 获取当前应用程序所分配的最大内存
private final int maxMemory = (int) Runtime.getRuntime().maxMemory();
// 只用五分之一用来做图片缓存
private final int cacheSize = maxMemory / 5;
private LruCache
cacheSize) {
// 重写sizeof()方法
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// TODO Auto-generated method stub
// 这里用多少kb来计算
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
}
};
public MyAdapter(Context context, List
this.context = context;
this.commondBeans = commondBeans;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return commondBeans.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return commondBeans.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
ViewHolder holder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(
R.layout.item_listview_commond, null);
holder = new ViewHolder();
view.setTag(holder);
holder.user_img = (ImageView) view.findViewById(R.id.userimg);
holder.user_detail = (TextView) view.findViewById(R.id.detail);
holder.user_name = (TextView) view.findViewById(R.id.name);
holder.user_good = (Button) view.findViewById(R.id.good);
holder.user_good.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "32个赞!", Toast.LENGTH_SHORT).show();
}
});
holder.user_poor = (Button) view.findViewById(R.id.poor);
holder.user_poor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "贬低一个!", Toast.LENGTH_SHORT).show();
}
});
holder.user_hot = (Button) view.findViewById(R.id.hot);
holder.user_hot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "更加火爆!", Toast.LENGTH_SHORT).show();
}
});
holder.user_share = (Button) view.findViewById(R.id.share);
holder.user_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "分享成功!", Toast.LENGTH_SHORT).show();
}
});
holder.good = (TextView) view.findViewById(R.id.goodtext);
holder.poor = (TextView) view.findViewById(R.id.poortext);
holder.hot = (TextView) view.findViewById(R.id.hottext);
holder.share = (TextView) view.findViewById(R.id.sharetext);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.user_detail.setText(commondBeans.get(position).getUserDetail());
holder.user_name.setText(commondBeans.get(position).getUserName());
holder.good.setText(commondBeans.get(position).getGood());
holder.poor.setText(commondBeans.get(position).getPoor());
holder.hot.setText(commondBeans.get(position).getHot());
holder.share.setText(commondBeans.get(position).getShare());
// 多任务处理加载图片
ListViewImageTaskUtil imageTask = new ListViewImageTaskUtil(holder.user_img);
imageTask.execute(commondBeans.get(position).getUserImg());
loadBitmap(commondBeans.get(position).getUserImg(), holder.user_img);
return view;
}
static class ViewHolder {
ImageView user_img;
TextView user_name;
TextView user_detail;
TextView good;
TextView poor;
TextView hot;
TextView share;
Button user_good;
Button user_poor;
Button user_hot;
Button user_share;
}
private void loadBitmap(String urlStr, ImageView image) {
ListViewImageTaskUtil asyncLoader = new ListViewImageTaskUtil(image,
mLruCache);// 一个异步图片加载对象
Bitmap bitmap = asyncLoader.getBitmapFromMemoryCache(urlStr);// 首先从内存缓存中获取图片
if (bitmap != null) {
image.setImageBitmap(bitmap);// 如果缓存中存在这张图片则直接设置给ImageView
} else {
asyncLoader.execute(urlStr);// 然后执行异步任务AsycnTask 去网上加载图片
}
}
重点就在activity和adapter中,每个listview中的点击的按钮的实例化需要在adapter中进行实例化,而不是在activity中进行实例化,还有每个listview中的图片的缓存都要在adapter进行相应的操作,具体的实现方法可以写一个工具类进行实现,里面用到了一些v4包中缓存的方法,大家可以了解一下,可以根据谷歌给的demo和上网查一下资料进行书写工具性方法,然后进行封装,这样以后那个项目需要就可以直接考进来用了