2012-07-01 5 views
1

내 응용 프로그램 필요의 SQLiteDatabase 테이블 갖는 2 개 개의 필드안드로이드 목록보기

1.sender ....

messagedb=context.openOrCreateDatabase("message",0, null); 
        messagedb.execSQL("CREATE TABLE IF NOT EXISTS tab2(sender INT(13),body varchar)"); 
        mydb.execSQL("INSERT INTO tab2 VALUES('"+sender+"','"+sb+"')"); 
//sb contains message 

2.message

어떻게

같은 데이터베이스에서 ListView를 채울 수 있습니다

http://a6.sphotos.ak.fbcdn.net/hphotos-ak-ash4/483993_349052115164919_754938581_n.jpg

그림 공간에는 연락처 사진이 포함되어 있습니다. 위의 텍스트 필드에는 mes를 배치하는 발신자 이름 (연락처 이름)이 들어 있습니다. sage .... 또한 메시지를받는 날짜와 시간을 추가하고 싶습니다 .... 이것에 대한 아이디어가 있습니까?

+0

MyCursorAdapter.java –

답변

0

사용자 지정 SimpleCursorAdapter 만들기. 필요한 열을 데이터베이스에 쿼리 할만큼 똑똑하다고 생각합니다. cusror가 준비되면 사용자 지정 어댑터로 전달하십시오.

다음 스 니펫이 도움이 될 것입니다.

cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, 
       null, null, null); 

contactList.setAdapter(new MyCursorAdapter(this, R.layout.row, cursor, 
       new String[] { ContactsContract.Contacts.DISPLAY_NAME, 
         ContactsContract.Contacts.LAST_TIME_CONTACTED }, 
       new int[] { R.id.name, R.id.email })); 

사진에 대한 URL이 더 이상 작동하지 않습니다

public class MyCursorAdapter extends SimpleCursorAdapter { 

    private Cursor cursor; 
    private int layout; 
    private Context context; 

    public MyCursorAdapter(Context context, int layout, Cursor cursor, 
      String[] from, int[] to) { 
     super(context, layout, cursor, from, to); 
     this.context = context; 
     this.cursor = cursor; 
     this.layout = layout; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     cursor.moveToPosition(position); <---- This is very important. 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(layout, null); 
     TextView txtName = (TextView) view.findViewById(R.id.name); 
     TextView txtEmail = (TextView) view.findViewById(R.id.email); 

     int nameColumnIndex = cursor 
       .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
     int frequencyColumnIndex = cursor 
       .getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED); 

     txtName.setText(cursor.getString(nameColumnIndex)); 
     txtEmail.setText(cursor.getString(frequencyColumnIndex)); 
     return view; 
    } 
} 
관련 문제