如何使用GridView设置可点击图像列表

时间:2014-04-22 15:47:41

标签: android gridview

如何使用GridView设置可点击图像列表?

这项工作的灵感来自本教程https://www.linux.com/learn/tutorials/748050-how-to-set-up-a-list-of-clickable-images-in-android-app-development-with-gridview

为什么我的GridView中出现此错误?

length cannot be resolved or is not a field

我的代码如下:

        final String[] myPics = new String[] { R.drawable.file1, R.drawable.file2, 
                                               R.drawable.file3, R.drawable.file4 };

        gridView = (GridView) findViewById(R.id.gridView1);
        gridView.setAdapter(new CustomAdapter(this, myPics));

        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(myPics.getItem(position).getImageUrlString()));
                         startActivity(i);          

            }
        });



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") };

public ImageWithUrl getItem(int position) {
    return imageIds[position];
}

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;
}
}

修改

CustomAdapter类

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

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("23/03/2014")) {
                imageView.setImageResource(R.drawable.file1);
            } else if (mobile.contains("09/03/2014")) {
                imageView.setImageResource(R.drawable.file2);
            } else if (mobile.contains("25/02/2014")) {
                imageView.setImageResource(R.drawable.file3);
            } else if (mobile.contains("16/02/2014")) {
                imageView.setImageResource(R.drawable.file4);
            } else if (mobile.contains("13/02/2014")) {
                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 Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

编辑2

CustomAdapter类

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("23/03/2014")) {
                imageView.setImageResource(R.drawable.file1);
            } else if (mobile.contains("09/03/2014")) {
                imageView.setImageResource(R.drawable.file2);
            } else if (mobile.contains("25/02/2014")) {
                imageView.setImageResource(R.drawable.file3);
            } else if (mobile.contains("16/02/2014")) {
                imageView.setImageResource(R.drawable.file4);
            } else if (mobile.contains("13/02/2014")) {
                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;
        }
    }
}

活动类

final String[] myPics = new String[] { R.drawable.file1, R.drawable.file2, 
                                       R.drawable.file3, R.drawable.file4 };

    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);          

            }
        }); 

1 个答案:

答案 0 :(得分:1)

您的错误似乎出现在onItemClick方法中。

您在此行中使用了一个getItem:

i.setData(Uri.parse(myPics.getItem(position).getImageUrlString()));

但如果您希望项目位于myPics数组中的“position”位置,则myPicsString[],您需要检索它:myPics [position]。

但是,您想要做的是将对象放在适配器中的位置,所以您需要做的是:

gridView = (GridView) findViewById(R.id.gridView1);
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(myAdapter.getItem(position).getImageUrlString()));
    startActivity(i);          

    }
});

在适配器中,实现getItem方法:

@Override
public String getItem(int position) {
    if (mobileValues != null && mobileValues.length > position) {
        return mobileValues[position];
    } else {
        return "";
    }
}

编辑:

抱歉,我误读了错误:

在你的情况下,我想如果你这样做:

i.setData(Uri.parse(getItem(position).getImageUrlString()));

应该没问题。