2012-11-24 5 views
0

나는 phonenumber가 문자열 xx에로드되기를 원합니다. "... 어떻게 가능합니까 ?? 메서드간에 데이터를 공유하는 방법 ?? 메서드를 public으로 만들려고했으나 도움이되지 않았습니다 ... 나는 액티비티를 알고 있습니다. 주요 활동에서 시작 ... 그래서 어떻게 다른 방법에서 데이터를 가져온 다음 주요 활동을 처리 할 수 ​​주요 활동이 처리되기 전에 다른 방법으로 가치를 얻는 방법?

public class MsgcontactActivity extends Activity { 

    private static final int CONTACT_PICKER_RESULT = 1001; 
    public void doLaunchContactPicker(View view) 
    { 
     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); 
     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
    } 

    EditText phoneTxt; 
    TextView tv; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv=(TextView) findViewById(R.id.textView1); 
     String xx=phoneTxt.getText().toString(); 
     Toast.makeText(getApplicationContext(), xx,Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    { 
     String phone=""; 
     Cursor contacts=null; 
     try 
     { 
      if (resultCode == RESULT_OK) 
      { 
       switch (requestCode) 
       { 
        case CONTACT_PICKER_RESULT: 
         Uri result = data.getData(); 
         String id = result.getLastPathSegment(); 
         contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); 
         int phoneIdx = contacts.getColumnIndex(Phone.DATA); 
         if (contacts.moveToFirst()) 
         { 
          phone = contacts.getString(phoneIdx); 
          EditText phoneTxt=(EditText)findViewById(R.id.phoneno); 
          phoneTxt.setText(phone); 
         } 
         else 
         { 
          Toast.makeText(this, "error", 100).show(); 
         } 
         break; 
        } 
       } 
       else 
       { 
        Toast.makeText(this, "Warning: activity result not ok",50).show(); 
       } 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(this, e.getMessage(), 50).show(); 
      } 
      finally 
      { 
       if (contacts != null) 
       { 
        contacts.close(); 
       } 
      } 
     } 
    } 
} 

XML 코드는 : -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <EditText 
     android:layout_height="wrap_content" 
     android:id="@+id/phoneno" 
     android:inputType="text|phone" 
     android:layout_width="wrap_content" 
     android:layout_toLeftOf="@+id/Contacts" 
     android:layout_alignParentLeft="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/Contacts" 
     android:text="Contacts" 
     android:layout_alignParentRight="true" 
     android:onClick="doLaunchContactPicker"/> 
    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:id="@+id/textView1" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/phoneno" 
     android:layout_alignParentLeft="true"/> 

</RelativeLayout> 

다음을 시도해보십시오

public class MsgcontactActivity extends Activity { 

private static final int CONTACT_PICKER_RESULT = 1001; 
public void doLaunchContactPicker(View view) 
    { 
     Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI); 
     startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 

    } 

EditText phoneTxt; 
     Button b; 
TextView tv; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
       b=(Button) findViewById(R.id.button1); 
     tv=(TextView) findViewById(R.id.textView1); 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       doLaunchContactPicker(v); 

      } 
     }); 



     String xx=phoneTxt.getText().toString(); 
     tv.setText(xx); 
} 


    @Override 
    public void onActivityResult(int requestCode, int resultCode, 
      Intent data) { 
     { 
      String phone=""; 
      Cursor contacts=null; 
      try 
      { 
       if (resultCode == RESULT_OK) 
       { 
        switch (requestCode) 
        { 
        case CONTACT_PICKER_RESULT: 

        Uri result = data.getData(); 

        String id = result.getLastPathSegment(); 

        contacts=getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null); 

        int phoneIdx = contacts.getColumnIndex(Phone.DATA); 
        if (contacts.moveToFirst()) 
        { 

          phone = contacts.getString(phoneIdx); 

          EditText phoneTxt=(EditText)findViewById(R.id.editText1); 

          phoneTxt.setText(phone);  

         } 
        else 
        { 
         Toast.makeText(this, "error", 100).show(); 
         } 
        break; 
        } 

       } 
       else 
       { 

        Toast.makeText(this, "Warning: activity result not ok",50).show(); 

       } 
      } 
      catch (Exception e) 
      { 
       Toast.makeText(this, e.getMessage(), 50).show(); 
       } 
      finally 
      { 
       if (contacts != null) 
       { 
        contacts.close(); 
       } 
      } 
    } 
    } 

} 

HTML 코드

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"> 
    <requestFocus></requestFocus> 
</EditText> 
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/editText1" android:layout_marginLeft="33dp"></Button> 
<TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"></TextView> 
</RelativeLayout> 

답변

0

전화에서 onCreate에서 setContentView(R.layout.main); 후이 doLaunchContactPicker() 방법() 메소드 후, onCreate에서 doLaunchContactPicker 메소드를 호출하려고하면 다음 doLaunchContactPicker() 방법에서보기를 사용하지 않는 경우 메서드에서 매개 변수로 View을 전달하지 말고이 줄을 추가하십시오.

+0

나는 시도했다. XML 코드를 사용해 보았다. ?? – zyonneo

+0

위의 코드를 참조하십시오 .. "다음 시도" – zyonneo

+0

[채팅에 대한 토론 계속] (http://chat.stackoverflow.com/rooms/20027/discussion-between-zyonneo-and-igp) – zyonneo

0

바로 setContentView

+0

사실 내가 클릭 할 때 "연락처 버튼"... 연락처 목록이 올 것입니다. 연락처를 클릭하면 번호가 edittext 및 textview에 표시되어야합니다 .... – zyonneo

+0

혼란 스럽 습니다만, 버튼을 참조하지 마십시오. 위의 코드에서 아무 곳이나. – antew

+0

나는 HTML 코드를 붙여 넣었다. pls는 모양을 가지고있다. – zyonneo

관련 문제