質問

連絡先を含むリストビューを作成しました...

tab_contact_list.xml, 、listViewを含みます:

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

    <ListView
        android:id="@+id/tab_contact_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>


listView_detail_tab_contact_list.xml, 、listViewの詳細:

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


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:src="@drawable/defaultavatar" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


        <TextView
            android:id="@+id/contact_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:text="Who am I"
            android:textAppearance="?android:attr/textAppearanceLarge" />


        <TextView
            android:id="@+id/contact_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="000000000" />

    </LinearLayout> 

</LinearLayout>


defaultAvatar.png フォルダーにあります 描画可能 defaultavatar.png


そして、私はいくつかのクラスを持っています:

public class ContactStock {

    private String name;
    private String number;

    public ContactStock(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return this.name;
    }

    public String getNumber() {
        return this.number;
    }

}


public class ContactListAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public ContactListAdapter(Activity activity, List objects) {
        super(activity, R.layout.listview_detail_tab_contact_list, objects);
        this.activity = activity;
        this.stocks = objects;
    }

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

        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail_tab_contact_list, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.contact_name);

            sv.number = (TextView) rowView.findViewById(R.id.contact_number);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());

        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
    }
}


クラスでlistViewを表示してもらいます:

public class addlistfromcontact extends Activity {
    private ListView lst;
    private List<ContactStock> contactstock;
    private Cursor mCursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_contact_list);
        lst = (ListView) findViewById(R.id.tab_contact_list);
        contactstock = new ArrayList<ContactStock>();

        mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null,
                ContactsContract.Data.DISPLAY_NAME + " ASC");
        int number = mCursor.getColumnIndex(Phone.NUMBER);      
        int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
        while (mCursor.moveToNext()) {
            String phName = mCursor.getString(name);
            String phNumber = mCursor.getString(number);
            contactstock.add(new ContactStock(phName, phNumber));
        }
        lst.setAdapter(new ContactListAdapter(addlistfromcontact.this,
                contactstock));
    }

}


そして私の結果は次のようになります:

Listview results


次に、defaultavatar.pngの代わりに、各連絡先の画像を表示するにはどうすればよいですか?

役に立ちましたか?

解決

あなたには間違いがあると思います。

long phId=mCursor.getLong(id);

whileループの一部である必要があります。

他のヒント

モデルを使用して写真を保持してみてください。

public class ContactStock {

    private Bitmap picture;

    // your current code goes here       

    public void setPicture(Bitmap picture) {
        this.picture = picture;
    }

    public Bitmap getPicture() {
        return picture;
    }
}

ArrayAdapterでgetPicture()メソッドを実装していることを確認してください。名前と数字のオブジェクトのみを使用します。XMLには、既にImageViewがあります。ArrayAdapterに追加してください。ビットマップ??これは私のために働いた...

public Bitmap loadContactPhoto(ContentResolver cr, long id) { 
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); 
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri); 
    if (input == null) { 
return null; 
    } 
    return BitmapFactory.decodeStream(input); 
}

StackoverFlowメンバー「MurtingMissle」はこれをここに投稿しました。 連絡先の写真をロードするにはどうすればよいですか?間違ったミスルに感謝します。既に連絡先の名前と番号を取得できるため、IDを引くだけで、そのメソッドをContentResolverでプラグインして、リストに配置できるビットマップを返します!それが助けてくれることを願っています。

上記の答えはあなたにエラーを与えるかもしれません...ここに100%の動作コードがあります

Uri uri = Uri.parse(url);
                             Bitmap bitmap = null;
                            try {
                                bitmap = MediaStore.Images.Media.getBitmap(contexts.getContentResolver(), uri);
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                bitmap = null;
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                bitmap = null;
                            }


                            if(bitmap != null)
                                friend.setImageURI(uri);
                            else
                                friend.setImageResource(R.drawable.avatar_photo); 

ImageViewは友達です。

まず、ビットマップを追加する必要があります ContactStock クラス、これにより連絡先の写真が保存されます。

アダプタークラスの最後を次のコードに変更します。

        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());
        sv.image.setImageBitmap(currentStock.getPicture());


        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageView image;
    }

主な活動に(AddlistFromContact)次の関数を追加します。

public Bitmap getPhoto(long userId ) {
        Uri photoUri = null;
        ContentResolver cr = this.getContentResolver();
        photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contacts);
        if (photoUri != null) {
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                    cr, photoUri);
            if (input != null) {
                return BitmapFactory.decodeStream(input);
            }
        } else {

            return defaultPhoto;
        }

        return defaultPhoto;
    }

これであなたがする必要があるのはあなたに追加することだけです onCreate() もちろん、カーソルを定義した後、この2行を機能させます。



    int id=mCursor.getColumnIndex(Phone.CONTACT_ID);
    long phId=mCursor.getLong(id);

そして、次の方法でcontactStockのconstracturを呼び出します。

contactstock.add(new ContactStock(phName, phNumber,getPhoto(phId)));

私が助けてくれたことを願っています

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top