使用AsyncTask做异步任务以及BaseAdapter的getview方法的逻辑问题

昨天开始调试一个BUG,从早上调到下班,都没有发现错误在哪里。。晚上再继续,跟朋友讨论了很久,开始有点思路。。最开始以为是异步更新UI出现问题,但是经过修改代码测试,问题依然存在。。最后发现的问题却令人啼笑皆非,竟然是在BaseAdapter的getview方法中的if(){}else(){}语句有问题。。。
-------------------------------------------铭记铭记-----------------------------------------------
AsyncTask:
public MyAsyncTask(Context con,BaseAdapter adapter,int taskID,Long thread_id)
{
super();
this.con = con;
this.ctr = con.getContentResolver();
this.taskID = taskID;
this.thread_id = thread_id;
switch(taskID)
{
case 0:
inBoxActivityAdapter = (InBoxActivityAdapter) adapter;
break;
case 1:
break;
case 3:
break;
}
}
protected void onPreExecute() {
switch(taskID)
{
case 0://会话列表
System.out.println("调用onPreExecute");
//     pd=ProgressDialog.show(con, null, con.getResources().getString(R.string.wait));
    break;
}
super.onPreExecute();
}
protected Object doInBackground(Object... params) {
ArrayList<MyInboxPerson> myInboxPerson=new ArrayList<MyInboxPerson>();
switch(taskID)
{
case 0://获得收件箱信息
System.out.println("调用doInBackground");
myInboxPerson.clear();
    myInboxPerson=doTaskInboxPerson(myInboxPerson);
    inBoxActivityAdapter.setList(myInboxPerson);
    break;
    }
    publishProgress();
return null;
}
protected void onProgressUpdate(Object... values) {
switch(taskID)
{
case 0://获得收件箱信息
      System.out.println("调用onProgressUpdate");
      inBoxActivityAdapter.notifyDataSetChanged();//更新adapter
      break;
}
if(pd!=null)
{
pd.dismiss();
}
super.onProgressUpdate(values);
}
protected void onPostExecute(Object result) {
switch(taskID)
{
case 0:
System.out.println("调用onPostExecute");
break;
}
super.onPostExecute(result);
}
protected void onCancelled() {
super.onCancelled();
}
getview():
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("---------程序执行到这里------13----");
ViewHolder holder = null;
int unread = 0;
if(holder==null)
{
holder = new ViewHolder();//实例化控件
if(convertView == null)
{
convertView = LayoutInflater.from(ct).inflate(R.layout.inbox_listitem, null);
}
//初始化组件
holder.connecter_image = (ImageView) convertView.findViewById(R.id.connecter_image);
holder.connecter_name = (TextView) convertView.findViewById(R.id.connecter_name);
holder.connect_message = (TextView) convertView.findViewById(R.id.connect_message);
holder.connect_date = (TextView) convertView.findViewById(R.id.connecter_time);
holder.unread_image = (ImageView) convertView.findViewById(R.id.unread_image);
holder.unread_txt = (TextView) convertView.findViewById(R.id.unread_txt);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
SimpleDateFormat format = new SimpleDateFormat( "MM/dd HH:mm" );//格式化日期和时间
SimpleDateFormat formatime = new SimpleDateFormat("HH:mm");//格式化时间
SimpleDateFormat formatdate = new SimpleDateFormat("MM/dd");//格式化日期
unread = lt.get(position).getUnRead();//获得未读短信数量
Date today = new Date(System.currentTimeMillis());//转成Date型
Date datetime = lt.get(position).getDate();//获得发信息的时间
int smsNum = lt.get(position).getSmsNum();//获得一个会话中全部短信数量
//给控件入值
holder.connecter_image.setImageBitmap(lt.get(position).getContactIcon());
//Bitmap回收
if(lt.get(position).getContactIcon().isRecycled())
{
lt.get(position).getContactIcon().recycle();
}
holder.connecter_name.setText(lt.get(position).getPersonName()+"("+smsNum+")");
holder.connect_message.setText(lt.get(position).getPersonMessage());
if(formatdate.format(today).equals(formatdate.format(datetime)))
{
holder.connect_date.setText(formatime.format(datetime));
}else
{
holder.connect_date.setText(format.format(datetime));
}
            System.out.println("unread--------------->"+unread);
            Bitmap contactIcon = null;
            Bitmap unReadIcon = null;
            
if(unread != 0)
{
unReadIcon = BitmapFactory.decodeResource(ct.getResources(), R.drawable.unread);
// 初始化画布
int iconSize = 45;
contactIcon = Bitmap.createBitmap(iconSize, iconSize,
Config.ARGB_8888);
Canvas canvas = new Canvas(contactIcon);
// 拷贝图片
Paint iconPaint = new Paint();
iconPaint.setDither(true);// 防抖动
iconPaint.setFilterBitmap(true);// 用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
Rect src = new Rect(0, 0, unReadIcon.getWidth(),
unReadIcon.getHeight());
Rect dst = new Rect(0, 0, iconSize, iconSize);
canvas.drawBitmap(unReadIcon, src, dst, iconPaint);
// 在图片上创建一个覆盖的联系人个数
int contacyCount = unread;
// 启用抗锯齿和使用设备的文本字距
Paint countPaint = new Paint();
countPaint.setColor(Color.RED);
countPaint.setTextSize(25f);
countPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText(String.valueOf(contacyCount), iconSize - 35,
35, countPaint);
holder.unread_image.setImageBitmap(contactIcon);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
holder.connecter_name.getPaint().setFakeBoldText(true);
//Btimap回收
if(contactIcon.isRecycled())
{
contactIcon.recycle();
}
}
else
{
System.out.println("-----------holder.connecter_name------------");
holder.unread_image.setImageBitmap(null);  -------->对应if(){}else(){}的逻辑
holder.connecter_name.getPaint().setFakeBoldText(false);
}
return convertView;
}
                                         

                                                                     ----转载请添加原文地址
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值