简体   繁体   English

从OnClickListener启动ACTION_DIAL Intent-Android-Java

[英]Starting ACTION_DIAL Intent from OnClickListener - android - Java

I have a ListView which displays user information and a call button. 我有一个ListView ,其中显示用户信息和一个呼叫按钮。 Now I want to create a dial action, so I used this code in my getView method: 现在,我想创建一个拨号操作,因此我在getView方法中使用了以下代码:

   Button callB = (Button)  convertView.findViewById(R.id.bCall);
        callB.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + user.getPhoneNumber())));
                        }
        });

However, the method startActivity is not available within the OnClickListener . 但是,方法startActivityOnClickListener不可用。 How can I solve this? 我该如何解决?

Any help would be greatly appreciated. 任何帮助将不胜感激。

I am assuming this piece of code is in your ArrayAdapter, and the getView() method is for getting the view of each list item. 我假设这段代码在您的ArrayAdapter中,而getView()方法用于获取每个列表项的视图。 If that is the case then, you can store each user's phone number in a hashmap, mapping the button's pointer id to the number, and then have the array adapter itself implement View.OnClickListener to receive click events for views: 如果是这种情况,可以将每个用户的电话号码存储在哈希图中,将按钮的指针ID映射到该号码,然后让数组适配器本身实现View.OnClickListener来接收视图的点击事件:

public class myAdapter extends ArrayAdapter implements View.OnClickListener{
    HashMap<int, String> mMap;

    public myAdapter(){
        mMap = new HashMap<int, String>;
    }

    @Override
    public View getView(int position, View view, ViewGroup container){
        // initialize view . . .
        . . .

        Button callB = (Button)  convertView.findViewById(R.id.bCall);
        callB.setOnClickListener(this);
        mMap.put(callB.getId(), "phone-number-goes-here");

        . . .
    }

    @Override
    public void onClick(View v){
        if (mMap.containsKey(v.getId())){
            getActivity().startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mMap.get(v.getId())));
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM