首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ListView中启用电话呼叫

在ListView中启用电话呼叫
EN

Stack Overflow用户
提问于 2013-06-13 00:29:21
回答 1查看 694关注 0票数 1

在互联网上进行了大量的研究后,我找不到如何在列表视图中启用电话呼叫的答案。下面是我的示例代码:假设我有一个包含50家医院的列表,其中包含这些医院的电话联系方式

代码语言:javascript
复制
public class Medical extends ListActivity {

static final ArrayList<HashMap<String,String>> list = 
    new ArrayList<HashMap<String,String>>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bus_main);
    SimpleAdapter adapter = new SimpleAdapter(
        this,
        list,
        R.layout.rowview,
        new String[] {"title","address","phone"},
        new int[] {R.id.text1,R.id.text2, R.id.text3}
    );     
    populateList();
    setListAdapter(adapter);      



}

private void populateList() {
    HashMap<String,String> 
    map = new HashMap<String,String>();
    map.put("title","UHS Hospital");
    map.put("address", "Street 54");
    map.put("phone", "4077000");
    list.add(map);
    map = new HashMap<String,String>();
    map.put("title","Calvary Hospital");
    map.put("address", "Street 43");
    map.put("phone", "2362491");
    list.add(map);
}
}

如何根据医院的单个电话号码详细信息(text3)启用对所有listview条目的电话呼叫。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-13 00:44:13

将此方法添加到您的活动中:

代码语言:javascript
复制
public class Medical extends ListActivity {

static final ArrayList<HashMap<String,String>> list = 
        new ArrayList<HashMap<String,String>>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.rowview,
            new String[] {"title","address","phone"},
            new int[] {R.id.textView1,R.id.textView2, R.id.textView3}
        );     
        populateList();
        setListAdapter(adapter);      



    }
    protected void onListItemClick (ListView l, View v, int position, long id){
        super.onListItemClick(l,v,position,id);
        if(position>=0 && position<list.size()) {
            HashMap<String, String> tmp = list.get(position);
            if(tmp.containsKey("phone")) {
                String tel = tmp.get("phone");
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+tel));
                startActivity(callIntent);
            }
        }
    }
    private void populateList() {
        HashMap<String,String> 
        map = new HashMap<String,String>();
        map.put("title","UHS Hospital");
        map.put("address", "Street 54");
        map.put("phone", "4077000");
        list.add(map);
        map = new HashMap<String,String>();
        map.put("title","Calvary Hospital");
        map.put("address", "Street 43");
        map.put("phone", "2362491");
        list.add(map);
    }

  }

并添加

代码语言:javascript
复制
<uses-permission android:name="android.permission.CALL_PHONE" />

在您的清单中

注意:此代码不检查数字是否有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17070669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档