Search code examples
androidsqlitecursor

How to: Populating views in a layout from a single record returned by a cursor without using list view


I have an activity page which has textview fields(name , phoneNo) and some buttons. I query the database to get the details of a single customer(1 record). A cursor is returned. I want to show the values(name , phoneNo) returned in the textviews of this activity. How can I do that without using a listView. I don't need a list because only a single record is returned. It's like a profile page. But to use a cursor I think I need somekind of listView. How to do this ???? Thankz in advance

  String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};

      int[] toViews = {R.id.textView1, R.id.textView3,R.id.CustomerName};

       TakenJobDataBaseOpenHelper jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);

         Cursor cursor = jobDatabaseHelper.getSingleRecord(id);
         SimpleCursorAdapter cursoradapter = new SimpleCursorAdapter(this, 
        R.layout.activity_job_details, cursor, fromColumns, toViews, 0);

Solution

  • you don't have to use a listview. simply use it like this:

    if (cursor.moveToFirst()) {
        String name = cursor.getString(cursor.getColumnIndex('NAME'));
        String phoneNo = cursor.getString(cursor.getColumnIndex('phoneNo'));
        ((TextView)findViewById(R.id.textView1)).setText(name);
        ((TextView)findViewById(R.id.textView2)).setText(phoneNo);
    } else {
        // nothing found!
    }