开发者

SimpleCursorAdapter isnt binding data to listView

How do I bind information from my database to a TextView? The ListView Isnt showing the information from the Cursor. Do I need to use the adapter I have created? I'm trying to display the results in TextViews withing the List.

@Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactform);

        Cursor cursor = getCursor();

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                this,                                                                                                   //Context
                R.layout.contactform,                                                                                   //xml definintion of each listView item
                cursor,                                                                                                 //Cursor
                new String[开发者_C百科] {"FirstName", "LastName", "Phone", "Email"},                                               //Columns to select From
                new int[] {R.id.contact_firstname, R.id.contact_lastname, R.id.contact_phone, R.id.contact_email}       //Object to bind to
                );


    }


    private Cursor getCursor() 
    {           
        String TABLE_NAME = "exampleContacts";
        String[] FROM = {"_id", ""FirstName", "LastName", "Phone", "Email"} ;

        dbManager = new DatabaseManager(this);
        SQLiteDatabase db = dbManager.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, FROM, "ContactID=?", new String[] {PrContactID}, null, null, null);
        startManagingCursor(cursor);
        return cursor;
    }


It looks like you're pulling back a single record. In that case, you don't need to be using the SimpleCursorAdapter, as that is generally used for assigning multiple records to a ListView. Why not just grab the info you need and manually set them to your TextView(s)?

Also, you shouldn't need to use a manage cursor. Just get the values you need and then close the cursor like so:

TextView textView = (TextView) findViewById(R.id.textView01); 

Cursor cursor = db.query("exampleContacts", new String[] {"FirstName", "LastName", "Phone", "Email"}, "ContactID=?", new String[] {PrContactID}, null, null, null);
cursor.moveToFirst();

String text = cursor.getString(0) + " " + cursor.getString(1) + " "  + cursor.getString(2) + " " + cursor.getString(3);

textView.setText(text);

cursor.close();

Sorry for any typos... this code is just for example and hasn't been tested.


The reason it wasnt being mapped to the ListView is because setListAdapter(); wasnt being used.

 @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contactform);
    Cursor cursor = getCursor();

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            this,                                                                                                   //Context
            R.layout.contactform,                                                                                   //xml definintion of each listView item
            cursor,                                                                                                 //Cursor
            new String[] {"FirstName", "LastName", "Phone", "Email"},                                               //Columns to select From
            new int[] {R.id.contact_firstname, R.id.contact_lastname, R.id.contact_phone, R.id.contact_email}       //Object to bind to
            );

     setListAdapter(adapter);

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜