2012-12-10 2 views
0

나는 contactmanager.java를 표시하여 전화로 연락처를 읽고 표시합니다. 특정 연락처 목록을 클릭하면 정확한 담당자 세부 정보가 표시되지 않습니다. 왜 그럴까요?Android : 클릭 한 대상 객체의 세부 정보 표시

/* 
* Copyright (C) 2009 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.android.contactmanager; 

import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public final class ContactManager extends Activity implements OnItemClickListener 
{ 

    public static final String TAG = "ContactManager"; 

    private Button mAddAccountButton; 
    private ListView mContactList; 
    private boolean mShowInvisible; 
    //public BooleanObservable ShowInvisible = new BooleanObservable(false); 
    private CheckBox mShowInvisibleControl; 

    /** 
    * Called when the activity is first created. Responsible for initializing the UI. 
    */ 



    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 


     Log.v(TAG, "Activity State: onCreate()"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.contact_manager); 

     // Obtain handles to UI objects 
     mAddAccountButton = (Button) findViewById(R.id.addContactButton); 
     mContactList = (ListView) findViewById(R.id.contactList); 
     mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible); 

     // Initialise class properties 
     mShowInvisible = false; 
     mShowInvisibleControl.setChecked(mShowInvisible); 

     // Register handler for UI elements 
     mAddAccountButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Log.d(TAG, "mAddAccountButton clicked"); 
       launchContactAdder(); 
      } 
     }); 
     mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() 
     { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       Log.d(TAG, "mShowInvisibleControl changed: " + isChecked); 
       mShowInvisible = isChecked; 
       populateContactList(); 
      } 
     }); 

     mContactList = (ListView) findViewById(R.id.contactList); 
     mContactList.setOnItemClickListener(this); 

     // Populate the contact list 
     populateContactList(); 
    } 



    /** 
    * Populate the contact list based on account currently selected in the account spinner. 
    */ 
    private void populateContactList() { 
     // Build adapter with contact entries 
     Cursor cursor = getContacts(); 
     String[] fields = new String[] { 
       ContactsContract.Data.DISPLAY_NAME, 
     }; 


     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor, 
       fields, new int[] {R.id.contactEntryText}); 
     mContactList.setAdapter(adapter); 
    } 

    /** 
    * Obtains the contact list for the currently selected account. 
    * 
    * @return A cursor for for accessing the contact list. 
    */ 
    private Cursor getContacts() 
    { 
     // Run query 
     Uri uri = ContactsContract.Contacts.CONTENT_URI; 
     Log.i("Uri ContactsContract.Contacts.CONTENT_URI" + ContactsContract.Contacts.CONTENT_URI, null, null); 

     String[] projection = new String[] 
       { 
       ContactsContract.Contacts._ID, 
       ContactsContract.Contacts.DISPLAY_NAME, 
       //ContactsContract.Contacts.DISPLAY_PHONE 
       }; 
     String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'"; 
     //String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible.get() ? "0" : "1") + "'"; 
     String[] selectionArgs = null; 
     String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

     return this.managedQuery(uri, projection, selection, selectionArgs, sortOrder); 
    } 

    /** 
    * Launches the ContactAdder activity to add a new contact to the selected account. 
    */ 
    protected void launchContactAdder() 
    { 
     Intent i = new Intent(this, ContactAdder.class); 
     startActivity(i); 
    } 

    public void onItemClick(AdapterView<?> l, View v, int position, long id) { 
     Log.i("TAG", "You clicked item " + id + " at position " + position); 
     // Here you start the intent to show the contact details 
    // selected item 
     //String contactDetails = (String)(mContactList.getItemAtPosition(position)); 
     //Uri contactDetails = ContactsContract.Contacts.CONTENT_URI; 

     Cursor cursor = getContacts(); 
     //Cursor emailCur = getContacts(); 
     cursor.moveToPosition(position); 
     String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     int phone = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA)); 
     //String email = emailCur.getColumnName(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 


     // Launching new Activity on selecting single List Item 
     Intent i = new Intent(getApplicationContext(), SingleListContact.class); 
     SingleListContact.PutDetails(ContactsContract.Contacts._ID, name, phone,null); 

     Log.i("Show Contact Clicked: ", name); 
     // sending data to new activity 
     //i.putExtra("Contact Person", contactDetails); 
     startActivity(i); 

    } 


} 

그리고 당신이 수행 한 SingleListContent.java

package com.example.android.contactmanager; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.util.Log; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SingleListContact extends Activity{ 

    static int ContactPhone; 
    static String ContactID, ContactName, ContactEmail; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.single_list_contact_view); 

     EditText txtContact = (EditText) findViewById(R.id.editText1); 
     txtContact.setText(ContactName); 

//  EditText txtContact2 = (EditText) findViewById(R.id.editText2); 
//  txtContact2.setText(ContactPhone); 

     EditText txtContact3 = (EditText) findViewById(R.id.editText3); 
     txtContact3.setText(ContactEmail); 


// 
//  Intent i = getIntent(); 
//  // getting attached intent data 
//  String contact = i.getStringExtra("contact"); 
//  // displaying selected product name 
//  txtContact.setText(contact); 
     Log.e("test", ContactID +" & " + ContactName); 


    } 

    static void PutDetails (String id, String name, int phone, String email) 
    { 
     ContactID = id; 
     ContactName = name; 
     ContactPhone = phone; 
     ContactEmail = email; 


    } 
} 

답변

1

그것이 작동하지 않습니다 그 이유는 완전히 잘못.

첫 번째 코드 단편에서 PutDetails 메소드에서 잘못된 값을 전달 중입니다.

당신이 전달하는 이름에 대해 ContactsContract.Data.DISPLAY_NAME은 이름이 아니며 전화 번호부의 contactName 열을 나타내는 문자열입니다. 따라서 이름을 얻으려면 다음을 사용하십시오.

cursor.moveToPosition(position); 
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

이메일에 대해서는 null 값을 보내므로 이메일은 확실히 null입니다. 연락처에 대한 이메일을 비슷하게 사용하려면

String email = emailCur.getString(
       emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
String emailType = emailCur.getString(
       emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 

여기에서 나는 당신이 이름과 이메일만을 사용하고 있음을 알 수 있습니다. 그러나 당신이 전화 번호를 원한다면, 당신은 전화 번호를 보내지 않고 단지 여기에 있기 때문에 그것을 찾아야 만합니다.

+0

어디 두 커서가 위치해야 .... 그리고 문자열 이름? contactmanager.java에? –

+0

SharedPreference와 PutDetails의 차이점은 무엇입니까? –

+1

죄송합니다. 마지막 코멘트를 작성하는 동안 실수를하였습니다. 나는 실제로 Putextra가 sharedpreference가 아니라고 쓰고 싶었다. 여기서 정적 설정 메소드를 사용하는 대신 Intent.Putextra를 사용하여 값을 다음 활동으로 쉽게 전달할 수 있습니다. 이것은 좋은 습관입니다. getextra를 호출하여 두 번째 활동에서 데이터를 가져올 수 있습니다. 이러한 메소드의 경우 android api docs를 참조하십시오. – stinepike

관련 문제