Tuesday, April 22, 2014

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: http://ift.tt/1iGkWpF




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://ift.tt/1iFZVvl"),
new ImageWithUrl(R.drawable.file2, "http://ift.tt/1iFZVvp"),
new ImageWithUrl(R.drawable.file3, "http://ift.tt/1muPTO9"),
new ImageWithUrl(R.drawable.file4, "http://ift.tt/1iFZVLG") };

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

}
});







No comments:

Post a Comment