Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
single list view in xml file and two arraylist list are used in java class how to fetch the value on the longclick of the item in list view from 2nd arraylist corrosponds to the first aarraylist.(like contact number and contact name only the contact name will be shwn on the list view and when you long press on the name the context menu will show the 3 options Call, SMS and Delete... when you click on call the phonecall activity will be started and also for sms and delete button ).
Java
package com.example.optionmenu;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ArrayList<String> name = new ArrayList<String>();
    ArrayList<String> phonenumber = new ArrayList<String>();
    ArrayAdapter<String> adapter;
    ListView listcon;
    String corrosphonenumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listcon=findViewById(R.id.listView);

        name.add("Rahul");
        name.add("Ishan");
        name.add("Manoj");
        name.add("Modi");

        phonenumber.add("1234567890");
        phonenumber.add("0987654321");
        phonenumber.add("1111111111");
        phonenumber.add("2222222222");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
        listcon.setAdapter(adapter);
        registerForContextMenu(listcon);

    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if(v.getId()==R.id.listView){
            AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo) menuInfo;

        }
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        menu.setHeaderTitle("Choose Action");


    }
    @Override
    public boolean onContextItemSelected(MenuItem item){
        if(item.getItemId()==R.id.call){
             Toast.makeText(getApplicationContext(),"Calling",Toast.LENGTH_LONG).show();
        }
        else if(item.getItemId()==R.id.sms){
            Toast.makeText(getApplicationContext(),"sending sms",Toast.LENGTH_LONG).show();
        }else if(item.getItemId()==R.id.dlt) {
            Toast.makeText(getApplicationContext(), "delete", Toast.LENGTH_LONG).show();
        }
        else {
            return false;
        }
        return true;
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.menu2,menu);
        menu.add("Add Contacts").setIcon(
                R.drawable.iii);
        return true;

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Intent intent=new Intent(MainActivity.this,addContact.class);
        startActivityForResult(intent,1);
        return super.onOptionsItemSelected(item);

        /*phoneNumber = editEnterNo.getText().toString();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + phoneNumber));*/
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}


What I have tried:

i am confuse how to access the elements and set the phone call to the corrosponding number
Posted
Updated 25-Feb-20 19:26pm
v2
Comments
David Crow 26-Feb-20 8:19am    
In your long-click handler, a position parameter will be passed. Use that as the index into the name and phonenumber arrays.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900