Getting Error In SetGridViewItemClickListener And Also How Can Show Images From Drawable
Solution 1:
You need to include the ImageActivity in the AndroidManifest.
Solution 2:
I have done a similar project with you and I use this way:
private ArrayList<String> urlImage = new ArrayList<>();
Remember, add data to urlImage
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), ShowImageActivity.class);
intent.putExtra(KEY_URL_IMAGE, urlImage);
intent.putExtra(KEY_POSITION, String.valueOf(position));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
In ShowImageActivity:
Intent intent = getIntent();
if (intent.getExtras() != null) {
url = (ArrayList<String>) intent.getExtras().get(KEY_URL_IMAGE);
position = Integer.parseInt(String.valueOf(intent.getExtras().get(KEY_POSITION)));
Toast.makeText(getApplicationContext(), "URL image" + url.get(position) + "position " + position, Toast.LENGTH_LONG).show();
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), url));
final ViewPager.LayoutParams layoutParams = new ViewPager.LayoutParams();
layoutParams.width = ViewPager.LayoutParams.MATCH_PARENT;
layoutParams.height = ViewPager.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.BOTTOM;
viewPager.setCurrentItem(position);
and show image from drawable
imageView.setImageResource(getImageId(view.getContext(),resourceImage));
public static int getImageId(Context context, String imageName) {
return context.getResources().getIdentifier("drawable/" + imageName, null, context.getPackageName());
}
Solution 3:
You are passing OnItemClickListener
in the place of context. You need to use the instance of the class like below.
Intent intent = new Intent(MainActivity.this, ImageActivity.class);
Note: If you use this
, then it would have the instance of the class it is written in. In your code, you are using this
inside an anonymous class of OnItemClickListener
Also, you need to check if you have defined your activity in the manifest file.
Post a Comment for "Getting Error In SetGridViewItemClickListener And Also How Can Show Images From Drawable"