如何在ItemClickListener中切换活动?

时间:2017-11-04 09:20:00

标签: android android-activity

在我的应用中,我使用listView来使用四个活动来显示日期。 我尝试过很多东西,但是我是android的新手。请帮助我完成我的第一个app.thanks帮助 这是我的代码。如何在活动之间切换?

resultListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id)
                    {



                            Intent j = new Intent(getBaseContext(), book_content.class);
                            String my_id1 = resultBooks.get(position).get("id").toString();

                            j.putExtra("id", my_id1);

                            startActivity(j);


                            Intent k = new Intent(getBaseContext(),book_content2.class);
                            String my_id2 = resultBooks.get(position).get("id").toString();

                            k.putExtra("id", my_id2);

                            startActivity(k);


                            Intent q = new Intent(getBaseContext(),book_content3.class);
                            String my_id3 = resultBooks.get(position).get("id").toString();

                            q.putExtra("id", my_id3);

                            startActivity(q);


                            Intent i = new Intent(getBaseContext(), book_content4.class);
                            String my_id4 = resultBooks.get(position).get("id").toString();

                            i.putExtra("id", my_id4);

                            startActivity(i);



                        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    }
                }
        );

1 个答案:

答案 0 :(得分:0)

列表OnItemClick给出行的位置,如果您映射列表位置,则可以导航到其他活动。只是改变它:

resultListView.setOnItemClickListener(
        new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                if (position == 0) {
                    Intent j = new Intent(getBaseContext(), book_content.class);
                    String my_id1 = resultBooks.get(position).get("id").toString();
                    j.putExtra("id", my_id1);
                    startActivity(j);
                } else if (position == 1) {
                    Intent k = new Intent(getBaseContext(), book_content2.class);
                    String my_id2 = resultBooks.get(position).get("id").toString();
                    k.putExtra("id", my_id2);
                    startActivity(k);
                } else if (position == 2) {
                    Intent q = new Intent(getBaseContext(), book_content3.class);
                    String my_id3 = resultBooks.get(position).get("id").toString();
                    q.putExtra("id", my_id3);
                    startActivity(q);
                } else if (position == 3) {
                    Intent i = new Intent(getBaseContext(), book_content4.class);
                    String my_id4 = resultBooks.get(position).get("id").toString();
                    i.putExtra("id", my_id4);
                    startActivity(i);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                }
            })
        }
);

快乐的编码!!