D
D
Dmitry Maslennikov2017-09-21 17:07:20
Android
Dmitry Maslennikov, 2017-09-21 17:07:20

How to pass post id from ListView to another Activity?

There is a ListView in which a brief description of the posts is loaded via REST. When you click on a list item, a second activity is called, which requests extended data on the publication from the server and displays it to the user.
I figured out the transfer of the post_id parameter itself. In the ListView, I hung a handler on the list items:

lv_posts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(Main.this, PostDetails.class);
        intent.putExtra("post_id", "61509");
        startActivity(intent);
    }
});

In the second activity I get the data like this:
Intent intent = getIntent();
post_id = intent.getStringExtra("post_id");

The question is how to bind the post_id from the first request (a list of posts) to the ListView elements and then how to pull them out of the desired element and pass them to putExtra (instead of the value "61509" there should be a post_id of a specific post).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mr_serg77, 2017-09-21
@aprenoir

Use RecyclerView (you implement it inside the ViewHolder and put an identifier when binding the view), identifiers can be implemented both in the Model class and through .setTag("your_tag");

P
Pavel Shvedov, 2017-09-21
@mmmaaak

Usually they create a special class, they call it ViewHolder, which then, using setTag(), you can save your ViewHolder instance in a specific ListViewItem, with the corresponding data, you can get it from item using getTag(), casting it to the desired ViewHolder class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question