Problem casting result from SQLite database into EditText
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(开发者_StackOverflow社区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.
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.
精彩评论