Android 数据库 - 将 Intent 字符串传递到光标中

发布于 2024-10-22 02:02:44 字数 1806 浏览 2 评论 0原文

我正在开发的应用程序是按联系人组织的笔记集合。我在使用游标查询数据库以获取正确的信息行时遇到问题。主要活动是基于联系人 URI 的联系人列表。单击联系人会将 Contact _ID 作为意图字符串传递。我想将字符串传递到光标中,以仅显示 contactId 键与意图字符串匹配的行,但每次传递字符串时,我都会收到“NumberFormatException:无法将 'null' 解析为整数”。我知道意图正在发生,因为我创建了一个菜单项,该菜单项使用其中的意图字符串生成一条 toast 消息,每次都可以正常工作,并且在使用 ID 对参数进行硬编码时光标可以工作。我就是不知道哪里出了问题!我只使用 Android 大约一个半星期,所以它很可能是非常简单的事情。非常感谢任何帮助。我的代码如下。

这是应该显示过滤后的数据行的活动:

public class NotesList extends ListActivity {
    public MyDbAdapter db;
    public String mIntentString, contactId;
    public int mIntentInt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = new MyDbAdapter(this);
        db.open();
        fillData();
        Bundle extras = getIntent().getExtras();
        mIntentString = extras.getString("contactId");
        contactId = extras.getString("contactId");
    }

    private void fillData() {
        Cursor mCursor = db.fetchNotesForContact(contactId);
        startManagingCursor(mCursor);
        ListAdapter adapter = new SimpleCursorAdapter(
                this,
                android.R.layout.two_line_list_item,
                mCursor,
                new String[] {MyDbAdapter.KEY_TITLE, MyDbAdapter.KEY_DATETIME},
                new int[] {android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
    }

这是调用光标的代码,当将参数从“contactId”更改为“139”时,将返回联系人的注释:

public Cursor fetchNotesForContact(String contactId) {
    String[] columns = {KEY_ROWID, KEY_CONTACTID, KEY_TITLE, KEY_BODY, KEY_DATETIME};
    String projection = ("contactId = ?");
    String[] arguments = {contactId};
    return mDb.query(DATABASE_TABLE, columns, projection, arguments, null, null, null);
}

My app I'm working on is a collection of notes organized by Contacts. I am having a problem using the cursor to query the database for the correct rows of information. The main activity is a list of contacts based on the Contacts URI. Clicking a contact passes the Contact _ID as an intent string. I want to pass the string into the cursor to display only the rows where the contactId key matches the intent string, but every time I pass the string I get "NumberFormatException: unable to parse 'null' as integer." I know the intent is going through because I created a menu item that makes a toast message with the intent string in it, which works fine every time, and the cursor works when hard coding the argument with an ID. I just cant figure out where the something is going wrong! I've only been working with Android for probably a week and a half, so it very well could be something very simple. Any help is greatly appreciated. My code is below.

This is the activity that should display the filtered rows of data:

public class NotesList extends ListActivity {
    public MyDbAdapter db;
    public String mIntentString, contactId;
    public int mIntentInt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = new MyDbAdapter(this);
        db.open();
        fillData();
        Bundle extras = getIntent().getExtras();
        mIntentString = extras.getString("contactId");
        contactId = extras.getString("contactId");
    }

    private void fillData() {
        Cursor mCursor = db.fetchNotesForContact(contactId);
        startManagingCursor(mCursor);
        ListAdapter adapter = new SimpleCursorAdapter(
                this,
                android.R.layout.two_line_list_item,
                mCursor,
                new String[] {MyDbAdapter.KEY_TITLE, MyDbAdapter.KEY_DATETIME},
                new int[] {android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
    }

And this is the code for the cursor being called, and when changing the argument from "contactId" to say "139", that contacts' notes are returned:

public Cursor fetchNotesForContact(String contactId) {
    String[] columns = {KEY_ROWID, KEY_CONTACTID, KEY_TITLE, KEY_BODY, KEY_DATETIME};
    String projection = ("contactId = ?");
    String[] arguments = {contactId};
    return mDb.query(DATABASE_TABLE, columns, projection, arguments, null, null, null);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

彼岸花似海 2024-10-29 02:02:44

问题是,您在 contactId 获得其值之前调用 fillData() 方法。因此 fetchNotesForContact 中的 null 也是如此。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    db = new MyDbAdapter(this);
    db.open();
    //fillData(); move this
    Bundle extras = getIntent().getExtras();
    mIntentString = extras.getString("contactId");
    contactId = extras.getString("contactId");
    fillData(); //to here
}

希望有帮助。

The problem is, that you are calling fillData() method before contactId has its value. So its null in fetchNotesForContact as well.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    db = new MyDbAdapter(this);
    db.open();
    //fillData(); move this
    Bundle extras = getIntent().getExtras();
    mIntentString = extras.getString("contactId");
    contactId = extras.getString("contactId");
    fillData(); //to here
}

hope that helps.

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