在GridView

时间:2017-03-09 10:54:17

标签: android listview gridview

我设计了GridView和List视图之间的意图。当我单击gridview的项目时,将打开与listview相关的下一个intent。然后,当我单击列表视图的项目时,将打开新意图以显示与列表视图的每个项目相关的文本。

有一个带有arraylist的对象,该对象也在arraylist中。 Gridview显示列出arraylist的每个对象。 列表视图显示列出对象中的每个arraylist项目。

我得到了第一个girdview和第一个listview。

我有一个问题如何获取gridview和列表视图的每个位置。

以下是最后一个意图中的代码。

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            switch (position){

                case 0:
                    Toast.makeText(getApplicationContext(),"London",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,AttactivePlacesActivity.class);
                    intent.putExtra("position_griditem_one",position);
                    startActivity(intent);
                    break;

                case 1:
                    Toast.makeText(getApplicationContext(),"Paris",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(MainActivity.this,AttactivePlacesActivity.class)
                            .putExtra("position_griditem_two",position));
                    break;

        }
    });

AttactivePlacesActivity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                switch(position){

                    case 0:
                        Toast.makeText(getApplicationContext(),"London Object",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(AttactivePlacesActivity.this,AttactivePlaceObject.class);
                        intent.putExtra("position_listitem_one",position);
                        startActivity(intent);
                        break;

                    case 1: 
                        startActivity(new Intent(AttactivePlacesActivity.this,AttactivePlaceObject.class)
                                .putExtra("position_listitem_two",position));
                        break;


                }


            }
        });

AttactivePlaceObject

// get from a previous gridview to get each item of gridview
        GridView gridView = (GridView) findViewById(R.id.gridlist);

        // get from a previous listview to get each item of listview
        ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);


        //gridView.getItemIdAtPosition();

        //listView.getItemIdAtPosition();


        ImageView attractivePlaceImage = (ImageView) findViewById(R.id.attractive_place_image);

        TextView attractivePlaceInformation = (TextView) findViewById(R.id.attractive_place_detailed_information);


        ImageView location = (ImageView) findViewById(R.id.location_icon);


        attractivePlaceImage.setImageResource(
                cityAttactivePlaces.get(0).getAttactivePlaces().get(0).getmAttaticePlaceImageResourceId());


        attractivePlaceInformation.setText(cityAttactivePlaces.get(0).getAttactivePlaces().get(0).getPlaceDetailInformation());

我的目标 当我点击伦敦时,列出了有吸引力的伦敦地方。当我点击巴黎时,列出了巴黎迷人的地方。点击任何城市的任何有吸引力的地方,详细信息显示为最终意图。

如何获取gridview和listviews&#39;显示最后意图的位置。

1 个答案:

答案 0 :(得分:1)

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                       Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();

Intent intent = new Intent(AttactivePlacesActivity.this,AttactivePlaceObject.class);
                        intent.putExtra("position",position);
                        startActivity(intent);

                 }
            });

注意 - 意图应由活动发送,并且必须由活动接收。