startActivity运行listview中的第一个元素,但就是这样

时间:2014-04-12 14:09:57

标签: android

 feed
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(getApplicationContext(),"position is   "+ position, Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(getApplicationContext(), DetailActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    Log.d("listview", "variables:  "+ ((Location2) places.get(position)).getName()+" ");
                    Log.d("listview", "variables:  "+ ((Location2) places.get(position)).getImage()+" ");
                    Log.d("listview", "variables:  "+ ((Location2) places.get(position)).getDiscription()+" ");
                    i.putExtra("name", ((Location2) places.get(position)).getName());
                    i.putExtra("pic", ((Location2) places.get(position)).getImage());
                    i.putExtra("disc", ((Location2) places.get(position)).getDiscription());

                    getApplicationContext().startActivity(i);
                }
            });

所有数据都存在并打印在日志中,但新活动仅在位置0时开始

1 个答案:

答案 0 :(得分:0)

您的问题不太清楚。如果您仅在位置0上显示Toast消息,则似乎仅在您单击位置0时调用onItemClick方法。

也许,正如马可的建议,你可以试试这个:

feed
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(view.getContext(), "position is   " + position, Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(view.getContext(), DetailActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    Log.d("listview", "variables:  " + ((Location2) places.get(position)).getName() + " ");
                    Log.d("listview", "variables:  " + ((Location2) places.get(position)).getImage() + " ");
                    Log.d("listview", "variables:  " + ((Location2) places.get(position)).getDiscription() + " ");
                    i.putExtra("name", ((Location2) places.get(position)).getName());
                    i.putExtra("pic", ((Location2) places.get(position)).getImage());
                    i.putExtra("disc", ((Location2) places.get(position)).getDiscription());

                    view.getContext().startActivity(i);
                }
            });