如何执行在listview项目上调用特定号码

时间:2014-12-26 07:59:53

标签: java android android-listview

嘿,请任何人告诉我以下的答案

我正在尝试使用onContextItemSelected()为Listner提供listview上的项目...

以下是我的代码..出了点问题..pls建议一些帮助我的东西。

我想为列表中的每个项目执行不同号码的呼叫..

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;
}

}

1 个答案:

答案 0 :(得分:1)

我的另一个解决方案非常简单。

对于此侦听器的listview和onItemClick上的

setonitemclicklistener,您将获得呼叫和短信的位置列表和显示对话框。