java - ListView of images and String arrays not working -


i new android programming. can me out? testing how display images , string in list view using arrayadapter. tried base adapter in different method same issue, app crashes , never runs.

here code mainactivity.java

import android.app.activity; import android.content.context; import android.content.res.resources; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview;   public class mainactivity extends activity {     listview l;     int [] images = {r.drawable.img1,r.drawable.img2, r.drawable.img3, r.drawable.img4, r.drawable.img5, r.drawable.img6, r.drawable.img7, r.drawable.img8, r.drawable.img9, r.drawable.img10};     string [] titles;     string [] description;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         resources resources=getresources();         titles = resources.getstringarray(r.array.title);         description = resources.getstringarray(r.array.description);         l = findviewbyid(r.id.listview);         populaterows arrayadapter = new populaterows(this, titles,images, description);         l.setadapter(arrayadapter);      }     class populaterows extends arrayadapter<string>{         context context;         int [] images;         string[] titles;         string [] description;         populaterows(context context, string[] titles, int[] imgs, string[] description){             super(context,r.layout.custom_layout, r.id.textview, titles);             this.context=context;             this.images=imgs;             this.titles=titles;             this.description=description;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             layoutinflater inflater = (layoutinflater) context.getsystemservice(layout_inflater_service);             view row = inflater.inflate(r.layout.custom_layout,parent,false);              imageview imageview = row.findviewbyid(r.id.imageview);             textview textview =  row.findviewbyid(r.id.textview);             textview textview1 = row.findviewbyid(r.id.textview1);              imageview.setimageresource(images[position]);             textview.settext(titles[position]);             textview1.settext(description[position]);             return row;         }     } } 

and here activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.name.mypackage.mainactivity">      <listview         android:id="@+id/listview"         android:layout_width="wrap_content"         android:layout_height="wrap_content">      </listview>  </android.support.constraint.constraintlayout> 

and again here other custom_layout.xml file

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent">      <imageview         android:id="@+id/imageview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:srccompat="@mipmap/ic_launcher"         android:layout_marginstart="28dp"         android:layout_alignparenttop="true"         android:layout_alignparentstart="true"         android:layout_margintop="29dp" />      <textview         android:id="@+id/textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginstart="49dp"         android:text="textview"         android:layout_aligntop="@+id/imageview"         android:layout_toendof="@+id/imageview" />      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignbottom="@+id/imageview"         android:layout_alignstart="@+id/textview"         android:text="textview" />  </relativelayout> 

when run code, i'll following error , app crashes:

--------- beginning of crash 09-12 10:11:44.925 4706-4706/com.example.name.mypackagee/androidruntime: fatal exception: main process: com.example.name.mypackage, pid: 4706 java.lang.outofmemoryerror: failed allocate 286654476 byte allocation 4896858 free bytes , 248mb until oom @ dalvik.system.vmruntime.newnonmovablearray(native method) @ android.graphics.bitmapfactory.nativedecodeasset(native method) @ android.graphics.bitmapfactory.decodestream(bitmapfactory.java:609) @ android.graphics.bitmapfactory.decoderesourcestream(bitmapfactory.java:444) @ android.graphics.drawable.drawable.createfromresourcestream(drawable.java:1080) @ android.content.res.resources.loaddrawableforcookie(resources.java:2638) @ android.content.res.resources.loaddrawable(resources.java:2543) @ android.content.res.resources.getdrawable(resources.java:806) @ android.content.context.getdrawable(context.java:458) @ android.widget.imageview.resolveuri(imageview.java:826) @ android.widget.imageview.setimageresource(imageview.java:433) @ com.example.name.mypackage.mainactivity$populaterows.getview(mainactivity.java:55) @ android.widget.abslistview.obtainview(abslistview.java:2346) @ android.widget.listview.measureheightofchildren(listview.java:1281) @ android.widget.listview.onmeasure(listview.java:1188) @ android.view.view.measure(view.java:18809) @ android.support.constraint.constraintlayout.internalmeasurechildren(constraintlayout.java:934) @ android.support.constraint.constraintlayout.onmeasure(constraintlayout.java:973) @ android.view.view.measure(view.java:18809) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5954) @ android.widget.framelayout.onmeasure(framelayout.java:194) @ android.view.view.measure(view.java:18809) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5954) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1465) @ android.widget.linearlayout.measurevertical(linearlayout.java:748) @ android.widget.linearlayout.onmeasure(linearlayout.java:630) @ android.view.view.measure(view.java:18809) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5954) @ android.widget.framelayout.onmeasure(framelayout.java:194) @ com.android.internal.policy.phonewindow$decorview.onmeasure(phonewindow.java:2643) @ android.view.view.measure(view.java:18809) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2112) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1228) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1464) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1119) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6060) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:606) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.handler.handlecallback(handler.java:746) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5443) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:728) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:618)

code makes list appear without images, displays string arrays

public class mainactivity extends activity {     listview l;     //int [] images = {r.drawable.img1,r.drawable.img2, r.drawable.img3, r.drawable.img4, r.drawable.img5, r.drawable.img6, r.drawable.img7, r.drawable.img8, r.drawable.img9, r.drawable.img10};     string [] titles;     string [] description;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         resources resources=getresources();         titles = resources.getstringarray(r.array.title);         description = resources.getstringarray(r.array.description);         l = findviewbyid(r.id.listview);         populaterows arrayadapter = new populaterows(this, titles, description);         l.setadapter(arrayadapter);      }     class populaterows extends arrayadapter<string>{         context context;         //int [] images;         string[] titles;         string [] description;         populaterows(context context, string[] titles, string[] description){             super(context,r.layout.custom_layout, r.id.textview, titles);             this.context=context;             //this.images=imgs;             this.titles=titles;             this.description=description;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             layoutinflater inflater = (layoutinflater) context.getsystemservice(layout_inflater_service);             view row = inflater.inflate(r.layout.custom_layout,parent,false);              //imageview imageview = row.findviewbyid(r.id.imageview);             textview textview =  row.findviewbyid(r.id.textview);             textview textview1 = row.findviewbyid(r.id.textview1);              //imageview.setimageresource(images[position]);             textview.settext(titles[position]);             textview1.settext(description[position]);             return row;         }     } } 

java out of memory error means resolution of images high. view doesn't populated thats why receiving crashes.

adjust image resolution making drawable different screen resolution drawable--hdpi, drawable--xhdpi, drawable--xxhdpi, drawable--xxxhdpi.

instead of listview try using recycleview if image content more.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -