以下是ListView
中的联系人列表,当用户在任何联系人上长按时,当用户按send sms
选项时会出现带有选项send sms
的上下文菜单,然后sms应发送到选定的号码。这里的问题是发送短信时电话价值总是空的,我不知道为什么?应该选择数字而不是null。
public class MainActivity extends AppCompatActivity {
ListView listView;
Button sync;
ProgressDialog progressDialog;
String name, phone;
ArrayList<Contact_list> listitem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listitem = new ArrayList<Contact_list>();
listView = (ListView) findViewById(R.id.listViewID);
registerForContextMenu(listView);
sync = (Button) findViewById(R.id.syncID);
sync.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// GET CONTACTS DATA
GetContactsIntoArrayList();
listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));
Toast.makeText(MainActivity.this, "import", Toast.LENGTH_SHORT).show();
}
});
}
public void GetContactsIntoArrayList(){
//`GetContactsIntoArrayList` is executed here
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Call");
menu.add(0, v.getId(), 0, "Send SMS");
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
// info.position will give the index of selected item
int IndexSelected=info.position;
if(item.getTitle()=="Call")
{
// Code to execute when clicked on This Item
}
else if(item.getTitle()=="Send SMS")
{
// here phone values is always null why?
intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "sms:" + phone));
intent.putExtra( "sms_body", "hi" );
startActivity(intent);
// Code to execute when clicked on This Item
}
else
{
return false;
}
return true;
}
}