2

私のアプリケーションの一部では、オプションが選択されたときにすべての連絡先のリスト(電話番号を含む)を表示する必要があります。

ボタンが押されたときに呼び出されるアクティビティは次のとおりです。

package com.example.prototype01;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;

public class nominateContactsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nominatecontactslayout);
        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String contactName, contactTelNumber = "";
        String contactID;
        c.moveToFirst();
        for (int i = 0; i < c.getCount(); i++) {
            contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
            if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
                while (pCur.moveToNext()) {
                    contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                } 
            }
            Log.i("name ", contactName + " ");
            Log.i("number ", contactTelNumber + " ");
            c.moveToNext();

        }
    }
    }

ご覧のとおり、このコードは、受話器に保存されているすべての連絡先の名前と電話番号を返します。現在、これらは単にlogcatにエコーされます。名前と番号だけを表示して、代わりにリストビューにこれらのアイテムを一覧表示する方法を理解できないようです。私は役に立たないためにいくつかのチュートリアルに従ったので、しぶしぶ私はあなたの親切な援助を求めます。この質問には何度も答えられていると確信しているので、しぶしぶ言いますが、コードにソリューションを適用できないようです。

前もって感謝します!!

よろしく、アントワン

4

2 に答える 2

2

連絡先をlistViewに入れるために私がすることは次のとおりです

あなたの活動で:

private ListView mContactsListView;
private ListContactItemAdapter mContactsListAdapter;

this.mContactsListAdapter = new ListContactItemAdapter(this, R.layout.contact_row);

// after doing setContentView, assuming you have defined a listview in your layout file
this.mContactsListView= (ListView) this.findViewById(R.id.contactsListView);
// You may want to create a custom adapter, which I wrote below for showing you example
this.mContactsListView.setAdapter(this.mContactsListAdapter);

for (/* each contact */) {

    Contact contact = new Contact();
    contact.name = contactName;
    contact.number = contactNumber;

    this.mContactsListAdapter.add(contact);
}

// In order to refresh your list and make data appear
this.mContactsListAdapter.notifyDataSetChanged();

もちろん、私の例では、連絡先データを含むモデル オブジェクトが必要です。

public class Contact  {

public String name;
public String number;

}

そして、これは上記の Contact クラスを使用したカスタム アダプタである可能性があります。

public class ListContactItemAdapter extends ArrayAdapter<Contact> {

private int mLineLayout;
private LayoutInflater mInflater;

public ListContactItemAdapter(Context pContext, int pLineLayout) {
    super(pContext, pLineLayout);

    this.mLineLayout = pLineLayout;
    this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

static class ViewHolder {

    TextView contactName;
    TextView contactNumber;
}

@Override
public View getView(int pPosition, View pView, ViewGroup pParent) {

    ViewHolder holder;
    if (pView == null) {

        pView = this.mInflater.inflate(this.mLineLayout, null);

        holder = new ViewHolder();
        holder.contactName = (TextView) pView.findViewById(R.id.contactName);
        holder.contactNumber = (TextView) pView.findViewById(R.id.contactNumber);

        pView.setTag(holder);

    } else {
        holder = (ViewHolder) pView.getTag();
    }

    Contact contact = getItem(pPosition);
    if (contact != null) {

        holder.contactName.setText(contact.name);
        holder.contactNumber.setText(contact.number);
    }

    return pView;
}

}

その例では、リストビューの行を定義するレイアウトが必要です。これは contact_row.xml の例です。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/contactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="6dp"
    android:text="Contact name"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/contactNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/contactName"
    android:paddingLeft="6dp"
    android:text="number"
    android:textAppearance="?android:attr/textAppearanceSmall" />


</RelativeLayout>

私は試しませんでしたが、これはうまくいくはずです。とにかく、リストビューが基本的にどのように機能するかを理解していただければ幸いです。

于 2012-08-12T22:16:59.960 に答える
1

Here is one way to adapt what you already have. I added comments before everything I changed to explain along the way:

public class nominateContactsActivity extends Activity {
    // Add a list to keep all the "name: number" strings
    private List<String> mNameNumber = new ArrayList<String>();

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nominatecontactslayout);
        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String contactName, contactTelNumber = "";
        String contactID;

        // You only need to find these indices once
        int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
        int hasNumberIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);


        // This is simpler than calling getCount() every iteration
        while(c.moveToNext()) {
            contactName = c.getString(nameIndex);
            contactID = c.getString(idIndex);

            // If this is an integer ask for an integer
            if (c.getInt(hasNumberIndex)) > 0) {
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
                while (pCur.moveToNext()) {
                    contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    // Store the "name: number" string in our list
                    mNameNumber.add(contactName + ": " + contactTelNumber);
                } 
            }
        }

        // Find the ListView, create the adapter, and bind them
        ListView listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mNameNumber);
        listView.setAdapter(adapter);
    }
}
于 2012-08-12T22:02:04.780 に答える