2017-04-12 59 views
-1

我想創建一個聯繫人應用程序到目前爲止這麼好,(我是Android開發新手),但我努力與代碼的這一部分,我有AlertDialog,我想要的元素顯示(聯繫人的信息,選中),帶我到我的活動,(ActivitySMS.class準確),但雖然它似乎沒有問題,運行應用程序,並從AlertDialog中選擇出現的號碼,它結束。日誌沒有顯示值得探索和理解問題的地方。AlertDialog和意圖

public class AgendaActivity extends ListActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_agenta); 

    ContentResolver resolver = getContentResolver(); 
    Cursor mCursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

    String[] contacts = { 
      ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, 
      ContactsContract.Contacts.LOOKUP_KEY, 
      ContactsContract.Contacts._ID, 
    }; 

    int[] views = new int[]{ 
      android.R.id.text1 
    }; 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_2, 
      mCursor, 
      contacts, 
      views, 
      0); 

    // Bind to our new adapter. 
    setListAdapter(adapter); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    final Cursor phoneCursor = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", 
      new String[]{"" + id}, 
      null); 

    assert phoneCursor != null; 
    final int nTelephones = phoneCursor.getCount(); 
    final String[] telephones = new String[nTelephones]; 
    int x = 0; 

    while (phoneCursor.moveToNext()) { 
     int col = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     telephones[x++] = phoneCursor.getString(col); 
    } 

    //Cursor Close 
    phoneCursor.close(); 

    final AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Telephone Selection"); 
    builder.setItems(telephones, new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialogInterface, int item) { 

      String currentNumber = "smsto: " + (telephones[item]); 

      Intent sentIntent = new Intent(AgendaActivity.this, ActivitySMS.class); 
      sentIntent.setData(Uri.parse(currentNumber)); 



      startActivity(sentIntent); 
     } 
    }); 

    AlertDialog alert = builder.create(); 
    alert.show(); 
} 

有沒有辦法克服這個障礙?

+0

還是你應該張貼你的日誌。 –

回答

0

通過

public static final String EXTRA_CURRENT_NUMBER = "EXTRA_CURRENT_NUMBER"; //just a string constant, it can be whatever you want 
... 
sendIntent.putExtra(EXTRA_CURRENT_NUMBER, currentNumber); 

替換行sendIntent.setData(Uri.parse(currentNumber));以後,您可以讀取該值在您的活動這樣

//ActivitySMS.class 
getIntent().getExtras().getString(MyDialogClass.EXTRA_CURRENT_NUMBER, "some default value...");