views:

56

answers:

1

Hello.

I have a database with 3 tables in it. They are "Projects", "Contacts" and "Project Types". I have a UI for creating (adding a record) a new project which adds a record to the "Projects" table. In that UI, I have a button to fetch Contact Names from the "Contacts" table which in turn displays a ListView of all Contacts. When the name is selected, it should go back to the Projects UI and the name has to be displayed in the EditText. However, i keep getting this in the EditText: android.database.sqlite.SQLiteCursor@43d34310

Am attaching code snippets to the post for advice.

The ListActivity onCreate() code:

public class ContactsList extends ListActivity {
DBAdapter dbContactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editproject_list);

    dbContactList = new DBAdapter(this);
    // Pull the data from the database
    String[] fields = new String[]    {    dbContactList.TABLE_CON_NAME    };
    int[] views = new int[]    {    android.R.id.text1    };

    Cursor c = dbContactList.getAllContacts();
    startManagingCursor(c);

    // Set the ListView
    ListAdapter prjName = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, c, fields, views);
    setListAdapter(prjName);

    dbContactList.close();
}

The onListItemClick code:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked

    Intent selectedContact = new Intent();

    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();

    selectedContact.putExtra("selectedcontact", keyword);

    setResult(RESULT_OK, selectedContact);
    finish();

}

And the Projects UI code to start the activity for the ListView and for getting the results:

public void onClickContactPicker(View target)
{
    Intent contactPicker = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
    startActivityForResult(contactPicker, contact);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String contact = data.getExtras().getString("selectedcontact").toString();

    ((EditText)findViewById(R.id.txtProjectContacts)).setText(contact);
}

This is the query() from the DB Adapter

public Cursor getAllContacts()
{
    Cursor cursor = null;

    try
    {
        cursor = db.query(TABLE_CONTACTS, new String[] {  
                TABLE_CON_ID, TABLE_CON_NAME, TABLE_CON_EMAIL,
                TABLE_CON_EXPERTISE, TABLE_CON_CHARGES}, 
                null,
                null, 
                null, 
                null, 
                null);
    }
    catch (SQLiteException e)
    {
        Log.e("Database.addRow", "Database Error: " + e.toString());
        e.printStackTrace();
    }

    return cursor;
}

Please advice on the solution to this.

Thanks in advance.

+2  A: 

Object o = this.getListAdapter().getItem(position);

At this point, o is a Cursor, positioned at the correct row.

String keyword = o.toString();

At this point, keyword is going to look something like android.database.sqlite.SQLiteCursor@43d34310, because that is what you get when you call toString() on a Cursor.

Perhaps you should be calling getString() with some column index instead.

CommonsWare
Does it mean I replace the o.toString() with o.getString(1)? I am still not too familiar with android development and am still quite a beginner as you would have found out by now. All help is appreciated really.
Siddharth Lele
@Siddharth Lele: If column `1` contains the value you want displayed in the `EditText`, then, yes, `o.getString(1)` would be what you want.
CommonsWare
I really am confused now sir. I do not have the option of o.getString at all. Is there an alternative solution that you can suggest instead of the one I am using? Perhaps a link to a tutorial of sorts? I was following a similar post where you have answered a similar question, but I really could not make heads or tails out of it: http://stackoverflow.com/questions/2075762/android-database-sqlite-sqlitecursor435b9ba0 . I realize that I am asking for too much perhaps.
Siddharth Lele
@Siddharth Lele: That is because you declared o to be an Object. Declare it to be a Cursor instead. If you are new to Java, I strongly encourage you to spend a couple of months learning Java before tackling Android. Here is a blog post where I outline what concepts in Java you will need to be an effective Android developer: http://commonsware.com/blog/2010/08/02/java-good-parts-version.html
CommonsWare
Oh my GOD. That worked like a charm. I cannot thank you enough sir. THANK YOU. YOU HAVE BEEN A LIFE SAVER SIR!!!!! I am also taking up your advice to learning Java. Your link has been bookmarked for good sir. Thanks a bunch yet again. :)
Siddharth Lele