将 SQLite 数据库的结果转换为 EditText 时出现问题
我有一个数据库,里面有 3 个表。它们是“项目”、“联系人”和“项目类型”。我有一个用于创建(添加记录)新项目的用户界面,该项目将记录添加到“项目”表中。在该用户界面中,我有一个按钮可以从“联系人”表中获取联系人姓名,该表又显示所有联系人的列表视图。选择名称后,它应该返回到项目 UI,并且名称必须显示在 EditText 中。但是,我不断在 EditText 中收到此消息:android.database.sqlite.SQLiteCursor@43d34310
我将代码片段附加到帖子中以获取建议。
ListActivity onCreate() 代码:
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();
}
onListItemClick 代码:
@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();
}
启动 ListView 活动并获取结果的项目 UI 代码:
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);
}
这是来自 DB Adapter 的 query()
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;
}
请提供有关解决方案的建议。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此时,
o
是一个Cursor
,位于正确的行。此时,
keyword
将类似于android.database.sqlite.SQLiteCursor@43d34310
,因为这就是您调用toString() 时得到的结果
位于光标
上。也许您应该使用某些列索引来调用
getString()
。At this point,
o
is aCursor
, positioned at the correct row.At this point,
keyword
is going to look something likeandroid.database.sqlite.SQLiteCursor@43d34310
, because that is what you get when you calltoString()
on aCursor
.Perhaps you should be calling
getString()
with some column index instead.