开发者

List View onClick passing _id

I have created a List view of all the Inbox emails of a user. Now on clicking the email id. I wanna have an activity where i display the content of the mail. Problem is i am not able to understand how to pass the ID of the mail to the activity to fetch that Email with Details.

public class InboxActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox);

        DBAdapter db = new DBAdapter(InboxActivity.this);
        db.open();
        long userID = Long.parseLong(MessagingApplication.getUserID());

        Cursor inbox = db.readInbox(userID);
        startManagingCursor(inbox);

        String[] mails = new String[] { DBAdapter.KEY_SUB };

        int[] to = new int[] { R.id.subject };

        SimpleCursorAdapter inboxmail = new SimpleCursorAdapter(this,
                R.layout.inbox_list, inbox, mails, to);

        setListAdapter(inboxmail);

        db.close();
    }
}

T开发者_StackOverflow社区his is my Inbox as List View


Take a look at the Android SDK Notebook example. You'll see there that they are using an Intent to pass data around (data in your case being URIs pointing to your mails).

You can have something like the code below to construct a mailUri (based on the selected item in your list), and start a new activity by passing on a new intent containing an Action (Intent.ACTION_VIEW) and Data (mailUri).

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri mailUri = ContentUris.withAppendedId(getIntent().getData(), id);
    startActivity(new Intent(Intent.ACTION_VIEW, mailUri));
}

Using Itents makes your code more loosely coupled and promotes re-use (notice how you don't specify the activity name that needs to be started).

Check the android fundamentals on Intent Filters to see how you can configure an activity to act upon your ACTION_VIEW mailUri.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜