将 SQLite 数据库的结果转换为 EditText 时出现问题

发布于 2024-09-25 16:11:38 字数 2686 浏览 1 评论 0原文

我有一个数据库,里面有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2024-10-02 16:11:38

对象 o = this.getListAdapter().getItem(position);

此时,o 是一个Cursor,位于正确的行。

字符串关键字 = o.toString();

此时,keyword 将类似于 android.database.sqlite.SQLiteCursor@43d34310,因为这就是您调用 toString() 时得到的结果 位于光标 上。

也许您应该使用某些列索引来调用 getString()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文