0

我认为的值int index=data.getIntExtra("recordindex", 0)应该是 60,但我得到的值是 0,为什么?

我犯了什么错误?谢谢!

    protected void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.sms_tel_number);

         Button btnadd = (Button) findViewById(R.id.btnadd);
         btnadd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {               

                    Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
                    i.putExtra("recordindex", 60);
                    startActivityForResult(i, 50);
                }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case 50:
          if (resultCode == RESULT_OK) {
              if (data == null) {
                  return;
              }

              //------------------------------------------------             
              ContentResolver reContentResolverol = getContentResolver();
              Uri contactData = data.getData();

              int index=data.getIntExtra("recordindex", 0); //The value should be 60, but it's 0, why

                          Cursor cursor = managedQuery(contactData, null, null, null,   null);            
                          if (cursor.moveToFirst()) {
                    String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    Cursor phone = reContentResolverol.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    if (phone.moveToFirst()) {
                        String usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Toast.makeText(getApplicationContext(), index + " B " + usernumber, Toast.LENGTH_SHORT).show();
                }
             }             
             //------------------------------------------------
          }
          break;
        default:
          break;
        }
    }

}
4

1 回答 1

3

在这里,您创建了一个Intent iforACTION_PICK,您在其中传递了一个额外的值“recordindex”。当然,您可以使用 Intents 传递值。
但是你在里面接收的onActivityResult(int requestCode, int resultCode, Intent data)意图不是你的意图。这是一个不同的,不包含那个额外的字段。

查看您的代码,因为您需要在同一活动中使用“recordindex”并且不保持持久性,只需将其用作变量即可。

int recordindex=0;
  protected void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.sms_tel_number);

     Button btnadd = (Button) findViewById(R.id.btnadd);
     btnadd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {               

                recordindex=60;
                Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);

                startActivityForResult(i, 50);
            }
    });
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 50:
      if (resultCode == RESULT_OK) {
          if (data == null) {
              return;
          }

          //------------------------------------------------             
          ContentResolver reContentResolverol = getContentResolver();
          Uri contactData = data.getData();

          int index=recordindex; //Here it is
          .........
          .........
  }
于 2013-11-14T04:57:43.727 回答