webView的使用及其乱码问题的解决方案

今天在做即时通讯的时候由于要把消息文本的格式显示出来,所以我采用了使用webView的方法。那么由于SimpleAdapter无法自动把数据绑定在webView上所以我需要自己定义Adapter,难点也就在这里。我在这里把自己的adapter贴出来:

/**
* 自定义List的Adapter
*/
class RecordAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String,Object>>  listitem;
public RecordAdapter(Context context,ArrayList<HashMap<String, Object>> listitem){
this.context = context;
this.listitem = listitem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listItem.size();
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listItem.get(arg0);
}


@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}


@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
//取出数据
String content = listitem.get(arg0).get("content").toString();
String time = listitem.get(arg0).get("time").toString();
View tem;
if(arg1 == null){
RelativeLayout temRl = (RelativeLayout)View.inflate(context, R.layout.talkrecorditem, null);
WebView wView = (WebView)temRl.findViewById(R.id.itemcontent);
wView.getSettings().setDefaultTextEncodingName("utf-8");  
TextView tView = (TextView)temRl.findViewById(R.id.itemtime);

wView.loadData(content, "text/html", null);
tView.setText(time);
tem = temRl;
}else{
tem = arg1;
WebView wView = (WebView)tem.findViewById(R.id.itemcontent);
TextView tView = (TextView)tem.findViewById(R.id.itemtime);
wView.loadData(content, "text/html", null);
tView.setText(time);
}
return tem;
}


WebView的默认编码是“Latin-1” 所以这样在显示中文的时候会出现乱码的现象。所以需要对webVIew的默认编码进行设定,

wView.getSettings().setDefaultTextEncodingName("utf-8");  

其实这句话的作用就相当于网页的元信息,相当与一个meta。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值