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 ).
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);
}
@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