简体   繁体   中英

Onclicklistener moving to same activity in android

Hi in my application I am displaying image with text by using grid view layout.In this i used list view and frame layout for designing a xml.

In that i used 6 items named as about us,photo gallery etc Now My problem is if i click the about us i want to move to about us activity and if i click the photo gallery it will move to flickr activity class.

But if i click the any activity it's moving to same activity.Now i don't want like that i want to move to particular activity

MainActivity class

public class MainActivity extends Activity implements OnClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        final int pos = getIntent().getIntExtra("pos",0); 
        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new MyAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position,
                    long id) {
                //Grab the item position here and write further code
                 switch (pos) {
                 case 0:
                     Intent nextScreen = new Intent(getApplicationContext(), Aboutus.class);
                    startActivity(nextScreen);
                     break;
                 case 1:
                     Intent nextScreen1 = new Intent(getApplicationContext(), FlickrActivity.class);
                    startActivity(nextScreen1);
                     break;


            }
            }

        });
    }

    private class MyAdapter extends BaseAdapter
    {
        private List<Item> items = new ArrayList<Item>();
        private LayoutInflater inflater;

        public MyAdapter(Context context)
        {
            inflater = LayoutInflater.from(context);

            items.add(new Item("About Us", R.drawable.aboutus));
            items.add(new Item("Photo Gallery", R.drawable.photo));
            items.add(new Item("Veg Food", R.drawable.veg));
            items.add(new Item("Non Veg Food", R.drawable.nonveg));
            items.add(new Item("Location", R.drawable.contactus));
            items.add(new Item("Contact Us", R.drawable.contactus));

        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i)
        {
            return items.get(i);
        }

        @Override
        public long getItemId(int i)
        {
            return items.get(i).drawableId;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
               v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
               v.setTag(R.id.picture, v.findViewById(R.id.picture));
               v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);

            picture.setImageResource(item.drawableId);
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

    }

}
 gridView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
         //Grab the item position here and write further code
         switch (pos) {
             case 0:
                 Intent nextScreen = new Intent(getApplicationContext(), Aboutus.class);
                startActivity(nextScreen);
                 break;
             case 1:
                 Intent nextScreen1 = new Intent(getApplicationContext(), FlickrActivity.class);
                startActivity(nextScreen1);
                 break;


        }
    }

});

In this code you switch on pos which is constant final int pos = getIntent().getIntExtra("pos",0); either the passed pos or zero. However you should switch on the position parameter. See below!

gridView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
         //Grab the item position here and write further code
         switch (position) {
             case 0:
                 Intent nextScreen = new Intent(getApplicationContext(), Aboutus.class);
                startActivity(nextScreen);
                 break;
             case 1:
                 Intent nextScreen1 = new Intent(getApplicationContext(), FlickrActivity.class);
                startActivity(nextScreen1);
                 break;


        }
    }

});

This should then work!°

Use the position from the itemclicklistener method

 gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
             switch (position) {
             case 0:
                 Intent nextScreen = new Intent(getApplicationContext(), Aboutus.class);
                 startActivity(nextScreen);
                 break;
             case 1:
                 Intent nextScreen1 = new Intent(getApplicationContext(), FlickrActivity.class);
                 startActivity(nextScreen1);
                 break;


        }
        }

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM