【问题标题】:Android: Display Details of The Target Object ClickedAndroid:显示点击的目标对象的详细信息
【发布时间】:2012-12-10 02:42:00
【问题描述】:

我有 contactmanager.java 来显示读取并显示来自电话的联系人。当我单击某些联系人列表时,它不会显示确切的联系人详细信息。我可以知道为什么吗?

  /*
 * 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;


    }
}

【问题讨论】:

    标签: android parameter-passing click


    【解决方案1】:

    你做错了,这就是为什么它不起作用。

    在第一个代码 sn-ps 中,您在 PutDetails 方法中传递了错误的值。

    对于您传递的名称 ContactsContract.Data.DISPLAY_NAME 这不是名称,它是代表电话簿的 contactName 列的字符串。因此,要获得名称,请使用以下内容。

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

    对于电子邮件,您发送的是空值,因此电子邮件肯定为空。如果您想要联系人的电子邮件,请类似地使用

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

    在这里我可以看到您只使用姓名和电子邮件。但是,如果您想要电话号码,那么您也必须找到它,因为在这里您不会发送任何电话号码,而只会发送职位。

    【讨论】:

    • 我应该在哪里设置两个 cursor.position....和字符串名称?在contactmanager.java 中?
    • SharedPreference 和 PutDetails 有什么区别?
    • 抱歉,我在发表最后一条评论时犯了一个错误。我实际上想写 Putextra 而不是 sharedpreference。在这里,您可以轻松地使用 Intent.Putextra 将值传递给下一个活动,而不是使用静态 setter 方法。这是一个很好的做法。您可以通过调用 getextra 在第二个活动中获取数据。对于这些方法,请参阅 android api 文档。
    • 输入你的代码后,我仍然无法访问电子邮件,关于 emailCur 变量的任何想法?
    猜你喜欢
    • 2023-03-04
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多