Results 1 to 3 of 3
  1. #1
    MiguelMay is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default How to Set Up a List of Clickable Images with GridView

    Hello guys, I need your help.

    My work in Android is inspired from this tutorial: https://www.linux.com/learn/tutorial...-with-gridview

    I need when image in Android GridView is clicked redirect to link provided on CustomAdapter class in
    Java Code:
    private ImageWithUrl[] imageIds
    Now when click on a image in GridView I have the crash of application.

    My code follow, can you help me?

    CustomAdapter class
    Java Code:
    public class CustomAdapter extends BaseAdapter {
        private Context context;
        private final String[] mobileValues;
    
        public CustomAdapter(Context context, String[] mobileValues) {
            this.context = context;
            this.mobileValues = mobileValues;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            View gridView;
    
            if (convertView == null) {
    
                gridView = new View(context);
    
                gridView = inflater.inflate(R.layout.mobile, null);
    
                TextView textView = (TextView) gridView
                        .findViewById(R.id.grid_item_label);
                textView.setText(mobileValues[position]);
    
                ImageView imageView = (ImageView) gridView
                        .findViewById(R.id.grid_item_image);
    
                String mobile = mobileValues[position];
    
                if (mobile.contains("file1")) {
                    imageView.setImageResource(R.drawable.file1);
                } else if (mobile.contains("file2")) {
                    imageView.setImageResource(R.drawable.file2);
                } else if (mobile.contains("file3")) {
                    imageView.setImageResource(R.drawable.file3);
                } else if (mobile.contains("file4")) {
                    imageView.setImageResource(R.drawable.file4);
                } else if (mobile.contains("file5")) {
                    imageView.setImageResource(R.drawable.file5);
                } else {
                    imageView.setImageResource(R.drawable.file6);
                }
    
            } else {
                gridView = (View) convertView;
            }
    
            return gridView;
        }
    
        @Override
        public int getCount() {
            return mobileValues.length;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public String getItem(int position) {
            if (mobileValues != null && mobileValues.length > position) {
                return mobileValues[position];
            } else {
                return "";
            }
        }
    
            private ImageWithUrl[] imageIds = {
            new ImageWithUrl(R.drawable.file1, "http://example.com/link1.html"),
            new ImageWithUrl(R.drawable.file2, "http://example.com/link2.html"),
            new ImageWithUrl(R.drawable.file3, "http://example.com/link3.html"),
            new ImageWithUrl(R.drawable.file4, "http://example.com/link4.html") };
    
        protected class ImageWithUrl extends Object {
            private int imageId;
            private String imageUrlString;
    
            ImageWithUrl(int id, String url) {
                imageId = id;
                imageUrlString = url;
            }
    
            public int getImageId() {
                return imageId;
            }
    
            public String getImageUrlString() {
                return imageUrlString;
            }
        }
    }
    Activity class

    Java Code:
    final String[] myPics = new String[] { mystring1, mystring2, ... };
    
        gridView = (GridView) findViewById(R.id.gridView1);
        final CustomAdapter myAdapter = new CustomAdapter(this, myPics);
        gridView.setAdapter(myAdapter);
    
        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
    
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(getItem(position).getImageUrlString()));
                startActivity(i);          
    
                }
            });
    Last edited by MiguelMay; 04-22-2014 at 07:58 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: How to Set Up a List of Clickable Images with GridView

    I have the crash of application.
    Is there a stack trace in the LogCat that shows what happened?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MiguelMay is offline Member
    Join Date
    Apr 2014
    Posts
    2
    Rep Power
    0

    Default Re: How to Set Up a List of Clickable Images with GridView

    thank you, solved... error print in LogCat...

Similar Threads

  1. how to use gridview in Jsp?
    By Ashu in forum Eclipse
    Replies: 2
    Last Post: 09-09-2013, 05:54 PM
  2. Switching images based on a list or actionlistener
    By scottsational in forum Java Applets
    Replies: 1
    Last Post: 01-30-2013, 05:27 AM
  3. Trying to get a list that goes on to show images
    By ciara in forum CLDC and MIDP
    Replies: 4
    Last Post: 12-05-2012, 07:26 PM
  4. Replies: 1
    Last Post: 06-29-2011, 03:32 PM
  5. Ask Something About Gridview
    By kirayamato in forum Android
    Replies: 0
    Last Post: 02-11-2011, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •