Face Value
Face Value

Reputation: 49

How do I dial specific numbers when clicking on certain ListView items

I've done the hard part. I've set up a ListView that, when an Item is clicked the phone starts to make a call.

When I click on the first ListView item, the dialler will show the title First Item for a split second, but then to proceed to show a series of random numbers, which has something to do with the String value.

Essentially what I want is to have a certain phone number dialled when I click on a particular item in the ListView.

Is this possible? If so, how?

Hopefully this makes sense, let me know if any further clarification is required.

ListView simpleList;
    final String contactList[] = {"First Item", "Second Item", "Third Item"};

    simpleList = (ListView)findViewById(R.id.contact_list);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_contact_list_view, R.id.textView, contactList);
    simpleList.setAdapter(arrayAdapter);

    simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @SuppressLint("MissingPermission")
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String value = (String)parent.getItemAtPosition(position);

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + value));

            startActivity(callIntent);

        }
    });

Upvotes: 0

Views: 47

Answers (1)

Jorgesys
Jorgesys

Reputation: 126523

OK if you had the position you can use it as index of the element inside the array

 final String contactList[] = {"First Item", "Second Item", "Third Item"};
 final String phonesList[] = {"First Item", "Second Item", "Third Item"};

Use position to get the contact from the array:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //String value = (String)parent.getItemAtPosition(position);
    String contact = contactList[position];

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + phonesList[position])); //***
    startActivity(callIntent);
}

Upvotes: 1

Related Questions