Amol mali
Amol mali

Reputation: 1

How to perform Call a specific number on listview items

Hey please any one tell me the answer of the following

i am trying to give Listener to items on listview using onContextItemSelected()...

following is a my code ..something went wrong ..pls suggest something which help me.

i want to perform call for each item in list for different numbers..

ListView listview1;
String Contacats[] = { "Rohini", "sonali", "Archana", "Dipti", "Maitri" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview1 = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Contacats);
    listview1.setAdapter(adapter);
    registerForContextMenu(listview1);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("select the action");
    menu.add(0, v.getId(), 0, "Call");
    menu.add(0, v.getId(), 0, "SMS");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if(item.getTitle()=="Call"){ 
         if(listview1.getAdapter().getItem(0)=="Rohini")
         {
          Intent phoneIntent = new Intent(Intent.ACTION_CALL);
          phoneIntent.setData(Uri.parse("tel:91-909-600-3409"));

          try {
             startActivity(phoneIntent);
             finish();               
          } catch (android.content.ActivityNotFoundException ex) {
             Toast.makeText(MainActivity.this, 
             "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
          }
         }
          else if(listview1.getAdapter().getItem(0)=="Archana")
             {
              Intent phoneIntent = new Intent(Intent.ACTION_CALL);
              phoneIntent.setData(Uri.parse("tel:91-956-680-3479"));

              try {
                 startActivity(phoneIntent);
                 finish();               
              } catch (android.content.ActivityNotFoundException ex) {
                 Toast.makeText(MainActivity.this, 
                 "Call faild, please try again later.", Toast.LENGTH_SHORT).show();
              }
         }
         else
         {
        Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();
         }
    }    
    else if(item.getTitle()=="SMS"){  
        Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
    }else{  
       return false;  
    }    
  return true;    

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Upvotes: 0

Views: 108

Answers (1)

Lokesh
Lokesh

Reputation: 3334

I have one other solution for your problem its very easy.

setonitemclicklistener on listview and onItemClick of this listener you get position list and show dialog for call and sms.

Upvotes: 1

Related Questions