안드로이드 개발 질문/답변
(글 수 45,052)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = new ArrayList<HashMap<String,Object>>();
// list2 = new ArrayList();
listView = (ListView) findViewById(R.id.ListViewMain);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
cursor = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +" ASC");
int idCol = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
int nameCol = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int numCol = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
while(cursor.moveToNext()){
HashMap<String,Object> item = new HashMap<String, Object>();
item.put("id", cursor.getString(idCol));
item.put("name", cursor.getString(nameCol) );
item.put("tel", cursor.getString(numCol) );
list.add(item);
}
cursor2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String [] {ContactsContract.CommonDataKinds.Photo.PHOTO},null, null,null);
photoCol = cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO);
bt = new Bitmap[cursor2.getCount()];
int i=0;
while(cursor2.moveToNext()){
byte[] data = cursor2.getBlob(photoCol);
if(data!=null){
bt[i] = BitmapFactory.decodeByteArray(data, 0, data.length);
Log.i("ss",i + " is " + "Not Null");
}else{
bt[i] = BitmapFactory.decodeResource(getResources(), R.drawable.ic_menu_contact);
Log.i("ss",i + " is " + "Null");
}
i++;
//SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list, R.layout.customview, new String[]{"photo","name","tel"}, new int[]{R.id.ImageView01,R.id.TextViewName,R.id.TextViewTel});
// listView.setAdapter(adapter);
listView.setAdapter(new CustomAdapter(getApplicationContext(), R.layout.customview));
}
class CustomAdapter extends BaseAdapter{
Context ctx;
int layout;
LayoutInflater inflater;
public CustomAdapter(Context ctx, int layout){
this.ctx =ctx;
this.layout = layout;
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if(convertView==null){
convertView = inflater.inflate(layout, arg2, false);
}
textName = (TextView) convertView.findViewById(R.id.TextViewName);
textTel = (TextView) convertView.findViewById(R.id.TextViewTel);
imageView = (ImageView) convertView.findViewById(R.id.ImageView01);
imageView.setImageBitmap(bt[position]);
textName.setText(list.get(position).get("name").toString());
textTel.setText(list.get(position).get("tel").toString());
return convertView;
}
}전화번호부에 있는 사진과 [이름.전화번호] 을 출력하는데 가져오는 테이블이 다릅니다.이름쪽 커서를 getCount() 하면 80이 나온다 그러면 사진쪽 cursor.getCount를 하면 160이 나옵니다.
이 둘을 매칭할 수 있는게 Raw_Contact_id 라고 하는데 그걸 사용하는 방법을 알고 싶습니다..ㅠ




일단 저도 예전에 저렇게 해서 고생했던 기억이.. ㅋ
지금 cursor를 위해 어뎁터를 커스터마이징 한건데요.
getView은 array의 자료형을 출력할때 어뎁터를 커스터 마이징 한 것입니다.
cursor를 위한 어뎁터 커스터 마이징은
ResourceCursorAdapter 이걸 상속 받은다음 물론 베이스 어뎁터를 상속 받아도 되나 그냥 심플커서어뎁터 바로 위의것을 상속 받는게 아무래도 좋기 때문에..
bindView와 newView를 재정의 해주시면 됩니다.자세한 사항은 예전에 제가 참고했던 자료를 올려드립니다.
http://www.androidpub.com/591578 <-- 이분 게시물 보시고요
그래도 이해 안되시면
ApiDemo에서 퀵 컨텍츠라고 있거든요 그거 보세요 그게 사진이랑 이름 출력 하는 예제인데 거기꺼 그냥 글거다가 쓰면 됩니다.
물론 그것도 제가 말한 방식으로 작성되어있습니다.