Android: How can I add HTML links inside a ListView?

后端 未结 5 678
旧时难觅i
旧时难觅i 2020-12-04 23:19

How would I go about adding clickable links inside a ListView?

5条回答
  •  -上瘾入骨i
    2020-12-04 23:32

    The tricky part of listview is nothing within (for instance a TextView of a Button) is clickable!

    Basically you need two string arrays:

    1. names that users see on the list_view;
    2. hyperlinks that you want to direct them to go.

    In the array.xml:

    
        Google
        Yahoo
        Bing
        Baidu
    
    
        https://www.google.com
        https://www.yahoo.com
        https://www.bing.com
        https://www.baidu.com
    
    

    In the layout_search_provider.xml it contains a list view:

    
    

    In your activity:

    public class SearchProvider implements  AdapterView.OnItemClickListener {
        private ListView lv_search;
        private String[] names = getResources().getStringArray(R.array.search_provider_name_array);
        private String[] links = getResources().getStringArray(R.array.search_provider_link_array);
    
        //..
    
        @Override
        public View onCreateView(View v, String name, Context context, AttributeSet attrs) {
            lv_search= (ListView) v.findViewById(R.id.lv_search);
    
            ArrayAdapter sAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, names);
            lv_search.setAdapter(sAdapter);
            lv_search.setOnItemClickListener(this);
    
            return v;
        }
    
        @Override
        public void onItemClick(AdapterView adapterView, View view, int i, long l) {
            if(i

    When your list is dynamic, you can the following method(s) to update your listview.

    • move the code in onCreateView() into onResume().
    • sAdapter.notifyDataSetChanged();

提交回复
热议问题