0

これは非常に奇妙な問題です。コードを直接投稿します。

package com.asim.contactspull;

    public class Contact
    {
    private String name;
    private String number;

    public Contact(String sname, String snumber)
    {
        this.name = sname;
        this.number = snumber;
    }
    }

上記は私のカスタムクラスです。以下は私のMainActivityです:

package com.asim.contactspull;

public class MainActivity extends Activity
{
    ArrayList<Contact> contactList;
    ListView contacts;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        contactList = new ArrayList<Contact>();

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
        while (phones.moveToNext())
        {
            Contact c = new Contact(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)), phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            Log.d("Contact: ", "" + c.toString());
            contactList.add(c);
        }
        phones.close();

        contacts = (ListView) findViewById(R.id.contacts);
        ArrayAdapter<Contact> aa = new ArrayAdapter<Contact>(this, android.R.layout.simple_list_item_1, contactList);
        contacts.setAdapter(aa);
    }
}

上記のコードを実行すると、ListView に次のようなエントリが表示されます。

com.asim.contactspull Contact@405206e8

私は何を間違っていますか?

4

2 に答える 2

1

toString()クラスの適切な表現を取得するには、メソッドをオーバーライドする必要があります。

このような:

public String toString(){
   return this.name;
}
于 2012-11-01T07:41:17.543 に答える
1

問題は、クラスに2つの属性があるため、アダプターが両方(名前と電話)を出力していることだと思います。

1 つのオプションは、名前だけで配列の ArrayAdapter を持つことです。または、カスタム アダプターを記述します。一方、カスタム クラスのカスタム toString も問題を解決する可能性があります。

于 2012-11-01T07:42:07.937 に答える