llx2015 2015-10-22 06:23 采纳率: 0%
浏览 1836
已采纳

android listview item中超链接的实现

我想在listview的textview中实现超链接,但是没有反应,是怎么回事?
还有个问题,就是适配器里面的内容怎么执行了5遍?

public class FishWebActivity extends Activity {
private Context context;
private TextView text_title;
private ListView web_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fish_web);
context = this;
init();
fish_list();
}

private void init(){
    text_title = (TextView)findViewById(R.id.textView1);
    web_list = (ListView)findViewById(R.id.listView1);
}

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
private void fish_list(){

    HashMap<String,String> map1 = new HashMap<String,String>(); 
    HashMap<String,String> map2 = new HashMap<String,String>(); 
    HashMap<String,String> map3 = new HashMap<String,String>(); 

    map1.put("webName", "中国水产-微头条");
    map1.put("webAddr", "http://www.wtoutiao.com/author/fishery1958.html");
    map2.put("webName", "中国水产养殖网");
    map2.put("webAddr", "http://m.shuichan.cc/");
    map3.put("webName", "中国水产门户网");
    map3.put("webAddr", "http://3g.bbwfish.com/");
    //将表添加进list

    list.add(map1);
    list.add(map2);
    list.add(map3);

    WeblistAdapter weblistAdapter = new WeblistAdapter(context,list);
    web_list.setAdapter(weblistAdapter);

}

public class WeblistAdapter extends BaseAdapter implements ListAdapter {

    private Context context;
    private ArrayList<HashMap<String,String>> list;
    private LayoutInflater inflater;
    public WeblistAdapter(Context context,ArrayList<HashMap<String,String>> list) {
        super();

        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        webViewItem webViewItem = null;
        if (convertView == null) {
            webViewItem = new webViewItem();
            convertView = inflater.inflate(R.layout.fish_web_list_item, null, false);

            //webViewItem.webName = (TextView) convertView.findViewById(R.id.textUsrName);
            webViewItem.webAddr = (TextView)findViewById(R.id.textUsrAddr);
            //webViewItem.webPic = (ImageView)convertView.findViewById(R.id.webPic);
            convertView.setTag(webViewItem);
        } else {
            webViewItem = (webViewItem) convertView.getTag();
        }

        Log.e("list"+position, list.get(position).get("webName"));
        webViewItem.webAddr.setText(list.get(position).get("webName"));
        SpannableString sp = new SpannableString(list.get(position).get("webName"));
        sp.setSpan(new URLSpan(list.get(position).get("webAddr")),0, 7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
        webViewItem.webAddr.setText(sp);   
        webViewItem.webAddr.setMovementMethod(LinkMovementMethod.getInstance());  
        return convertView;
    }

    public class webViewItem {
        TextView webAddr;
        TextView webName;
        ImageView webPic;
    }

}

}
``


  • 写回答

2条回答 默认 最新

  • Lange0x0 2015-10-22 07:06
    关注
    webViewItem.webAddr = (TextView)findViewById(R.id.textUsrAddr);这句有问题,掉了convertView
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding