问题
I have created android app using GridView with ViewPager for images like gallery or small book which have 14 images
am showing all image from URL. i want also help that how can show my image from drawable
GridView got open successfully and i am able to launch the application.
Functionality is also working fine but when I want to open single image from GridView then my app close and showing message that app has stopped and open app again.
Please review my code and guide me how can I solve this problem. Here is my code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private GridView gridView;
private GridViewAdapter gridViewAdapter;
private ArrayList<String> listImageURLs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addImageURLs();
gridView = findViewById(R.id.gridView);
gridViewAdapter = new GridViewAdapter(this, listImageURLs);
gridView.setAdapter(gridViewAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setGridViewItemClickListener();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void setGridViewItemClickListener(){
gridView.setOnItemClickListener((parent, view, position, id) -> {
Bundle bundle = new Bundle();
bundle.putInt("position", position);
bundle.putStringArrayList("imageURLs", listImageURLs);
Intent intent = new Intent(this, ImageActivity.class);
intent.putExtras(bundle);
startActivity(intent);
});
}
private void addImageURLs(){
listImageURLs.add("http://i.dailymail.co.uk/i/pix/2016/04/12/23/3319F89C00000578-3536787-image-m-19_1460498410943.jpg");
listImageURLs.add("https://www.w3schools.com/css/img_forest.jpg");
listImageURLs.add("https://www.w3schools.com/css/trolltunga.jpg");
listImageURLs.add("https://www.w3schools.com/css/pineapple.jpg");
listImageURLs.add("https://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg");
listImageURLs.add("https://www.w3schools.com/css/paris.jpg");
listImageURLs.add("https://www.w3schools.com/css/paris.jpg");
listImageURLs.add("https://www.w3schools.com/css/trolltunga.jpg");
listImageURLs.add("https://www.w3schools.com/css/lights600x400.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-11.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-15.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-19.jpg");
listImageURLs.add("https://cdn.spacetelescope.org/archives/images/thumb700x/heic1509a.jpg");
listImageURLs.add("http://wallpaper-gallery.net/images/image/image-12.jpg");
//listImageURLs.addAll(listImageURLs);
}
}
回答1:
You need to include the ImageActivity in the AndroidManifest.
回答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());
}
回答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.
来源:https://stackoverflow.com/questions/50906577/getting-error-in-setgridviewitemclicklistener-and-also-how-can-show-images-from