开发者

Android Database - Passing Intent String into Cursor

My app I'm working on is a collection of notes org开发者_运维技巧anized 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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜