Reputation: 3890
[Edited]I wanted to make a listview of images and on clicking them another activity should open(a separate activity for each clickable) but the setOnItemClickListener is not working.
This is my Main Activity.java
package com.example.priyanshu.justforfun;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<Block> blocks = new ArrayList<Block>();
blocks.add(new Block(R.drawable.one,R.drawable.two));
blocks.add(new Block(R.drawable.three,R.drawable.four));
blocks.add(new Block(R.drawable.five,R.drawable.six));
blocks.add(new Block(R.drawable.seven,R.drawable.eight));
blocks.add(new Block(R.drawable.nine,R.drawable.ten));
ImageAdapter adapter = new ImageAdapter(MainActivity.this, blocks, R.color.gray);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,DemoActivity.class);
startActivity(intent);
}
}
);
This is Imageadapter.java-
public class ImageAdapter extends ArrayAdapter<Block> {
private int mColorResourceId;
public ImageAdapter(Activity context, ArrayList<Block> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Block currentBlock = getItem(position);
ImageButton iconView1 = (ImageButton) listItemView.findViewById(R.id.im1);
ImageButton iconView2 = (ImageButton) listItemView.findViewById(R.id.im2);
iconView1.setImageResource(currentBlock.getImage1ResourceId());
iconView2.setImageResource(currentBlock.getImage2ResourceId());
return listItemView;
}
}
This is Block.java-
public class Block {
private int mImage1ResourceId = NO_IMAGE_PROVIDED;
private int mImage2ResourceId = NO_IMAGE_PROVIDED;
private static final int NO_IMAGE_PROVIDED = -1;
public Block(int Image1ResourceId ,int Image2ResourceId) {
mImage1ResourceId = Image1ResourceId;
mImage2ResourceId = Image2ResourceId;
}
public int getImage1ResourceId(){ return mImage1ResourceId;}
public int getImage2ResourceId(){ return mImage2ResourceId;}
}
This is List_item.xml
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:id="@+id/list_item">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/background_splash"
android:id="@+id/im1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="@drawable/background_splash"
android:id="@+id/im2" />
</LinearLayout>
Upvotes: 0
Views: 57
Reputation: 6405
As CommonsWare said:
You are trying to allocate 74649612 bytes. This is equivalent to a 8640 x 8640-pixel image. This is much too large.
You have to use much smaller sized images in drawable as this allocates too much memory. Moreover, You can Use Glide/Picasso to load your Images.Glide handles bitmap decoding, disk caching efficiently to ensure lesser memory allocation and avoide out of memory error.
In order to use Glide to your project, First, Add this to dependency in app gradle file :
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:xx.x.x' // According to your compileSdkVersion
}
Inside your getView()
:
Replace:
iconView1.setImageResource(currentBlock.getImage1ResourceId());
iconView2.setImageResource(currentBlock.getImage2ResourceId());
With:
Glide.with(context)
.load(currentBlock.getImage1ResourceId())
.placeholder(R.drawable.placeholder) //Use low res image as placeholder
.error(R.drawable.imagenotfound) //Use low res image as error image
.into(iconView1);
Glide.with(context)
.load(currentBlock.getImage2ResourceId())
.placeholder(R.drawable.placeholder)
.error(R.drawable.imagenotfound)
.into(iconView2);
To open different activities on click different items use the following code:
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == 0){
Intent i = new Intent(MainActivity.this, NextActivity1.class);
startActivity(i);
}
else if(position == 1){
Intent i = new Intent(MainActivity.this, NextActivity2.class);
startActivity(i);
}
// Do as above for rest of the positions
}
});
Upvotes: 2
Reputation: 149
I think it's occurring for images. You need to load the high resolution image using Bitmap or any library. Or for simple way, add the images to drawable nodpi directory. It won't scale the image.
Upvotes: 0
Reputation: 5240
Having only 6 images shouldn't max out your memory. The only thing I can see making problem here is size of your images.
Did you create appropriate drawable folders with proper file size?
Upvotes: 0