0

listViewでconctactsを取得したいのですが、SocialAuthの1つのコードを再利用したいのです。このコードでは、LogCatでContactListを取得できますが、listViewでは取得できません。これを行う方法がわかりません。このlistViewは他のXML上にあります。

私はゴーグルなどをしていますが、コードを適応させる方法がわかりません

public void Events(String provider) {

    setContentView(R.layout.contact_list);

    List < Contact > contactsList = adapter.getContactList();




    if (contactsList != null && contactsList.size() > 0) {
        for (Contact p: contactsList) {

            if (TextUtils.isEmpty(p.getFirstName()) && TextUtils.isEmpty(p.getLastName())) {
                p.setFirstName(p.getDisplayName());
            }

            Log.d("Custom-UI", "Display Name = " + p.getDisplayName());
            String ContactNAme = p.getDisplayName();
            //ContactName = new String[] {p.getDisplayName()};
            //mapTo = new int[] {android.R.id.text1};
            Log.d("Custom-UI", "First Name = " + p.getFirstName());
            String ContactFisrtName = p.getFirstName();
            Log.d("Custom-UI", "Last Name = " + p.getLastName());
            String ContactLastName = p.getLastName();
            Log.d("Custom-UI", "Contact ID = " + p.getId());
            String ContactId = p.getId();
            Log.d("Custom-UI", "Profile URL = " + p.getProfileUrl());
            String ContactProfileUrl = p.getProfileUrl();

        }
        //  Log.d("ContactList",mAdapter.toString());
    }
    Toast.makeText(CustomUI.this, "View Logcat for Contacts Information", Toast.LENGTH_SHORT).show();


}

誰もいないのでカーソルが問題だと思いますが、この機能はカーソルのように機能すると思います

public List<Contact> getContactList() 
{
    try 
    {
        contactsList = new contactTask().execute().get();
    } 
    catch (InterruptedException e) 
    {
        e.printStackTrace();
    } 
    catch (ExecutionException e) 
    {
        e.printStackTrace();
    }

    return contactsList;
}

だから誰かが私を助けてくれるなら、どうもありがとう。

4

3 に答える 3

0

リストビュー用のカスタムアダプタを作成する必要があります。

これが良い例です

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

于 2012-06-26T11:29:31.973 に答える
0

以下のリンクをご覧ください

http://developer.android.com/tools/samples/index.html

これは、AndroidSDKのサンプル例です。ここで、目的の結果を得ることができます。

于 2012-06-26T11:32:24.807 に答える
0
  // Call contact thread 

    contact_thread = new Contact_thread();
        contact_thread.start();

 private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, null, selectionArgs, sortOrder);
}

class Contact_thread extends Thread {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        // Build adapter with contact entries
        Cursor cursor = getContacts();

        cursor.moveToFirst();
        contactName = new String[cursor.getCount()];
        contactNo = new String[cursor.getCount()];
        checkedPosition = new boolean[cursor.getCount()];



        ContentResolver contect_resolver = getContentResolver();
        int i = 0;
        if (cursor.getCount() > 0) {
            do {
                String id = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                Cursor phoneCur = contect_resolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);
                if (phoneCur.moveToFirst()) {
                    contactName[i] = phoneCur
                            .getString(phoneCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    contactNo[i] = phoneCur
                            .getString(phoneCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    if (contactName[i] == null) {
                        contactName[i] = "Unknown";
                    }

                } else {
                    contactName[i] = "Unknown";
                    contactNo[i] = "";
                }

                db.AddContact(contactName[i], contactNo[i]);

                i++;
                phoneCur.close();
            } while (cursor.moveToNext());


        }
        cursor.close();
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                // mContactList.setAdapter(cursorAdapter);

                mContactList.setAdapter(new ContactAdapter(
                        ContactManager.this, R.layout.contact_entry));


            }
        });
    }
}

private class ContactAdapter extends ArrayAdapter<String> implements
        Filterable {

    public ContactAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    public int getCount() {
        return contactName.length;
    }

    public String getItem(int position) {
        return contactName[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View v;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.contact_entry, null);
        } else {
            v = convertView;
        }
        CheckedTextView text = (CheckedTextView) v.findViewById(R.id.text1);
        text.setText(contactName[position] + "  (" + contactNo[position]
                + ") ");
        text.setChecked(checkedPosition[position]);
        return v;
    }

}

contact_entry.xml

  <?xml version="1.0" encoding="utf-8"?>
  <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:background="#AA0114"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />
于 2012-06-26T11:56:28.000 に答える