llx2015
llx2015
2015-10-22 06:23
采纳率: 60%
浏览 1.8k

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条回答 默认 最新

  • onTheRoadToMine
    Lange0x0 2015-10-22 07:06
    已采纳
    webViewItem.webAddr = (TextView)findViewById(R.id.textUsrAddr);这句有问题,掉了convertView
    
    点赞 评论
  • onTheRoadToMine
    Lange0x0 2015-10-22 06:44

    这个是点击事件的抢占问题

    点赞 评论

相关推荐