2012-07-01 83 views
1

有我有應用SQLiteDataBase表兩個字段Android的列表視圖

1.sender .... 2.message

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 

我怎麼能填充從數據庫中一個ListView像

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

圖片空間包含聯繫人圖片,上面的textfield包含發件人名稱(聯繫人名稱)下方的那個地方mes聖人....我也想添加消息收到的日期和時間也....任何想法呢?

+0

的URL畫面不工作了 –

回答

0

創建自定義SimpleCursorAdapter.I認爲您足夠聰明,可以查詢所需列的數據庫。 一旦您的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 })); 

MyCursorAdapter.java

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; 
    } 
}