java - Android App Crashing - List View - CSV file -



java - Android App Crashing - List View - CSV file -

ok, trying populate list view on load crashing.

importing product csv in list view in android studio.

here csv https://drive.google.com/file/d/0bycsf7wmf1cymzbomk1mzmliww8/view?usp=sharing

mainactivity:

import android.app.activity; import android.os.bundle; import android.widget.listview; public class mainactivity extends activity { listviewadapter itemadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview itemlist = (listview)findviewbyid(r.id.itemlistview); itemadapter = new listviewadapter(this, -1); itemlist.setadapter(itemadapter); } }

listviewadapter

import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; public class listviewadapter extends arrayadapter<datastates> { context contxt; public listviewadapter(context context, int testviewresourceid) { super(context, testviewresourceid); contxt = context; loadarraydata(); } @override public view getview (final int pos, view convertview, final viewgroup parent) { textview mview = (textview) convertview; if(mview == null) { mview = new textview(parent.getcontext()); mview.settextsize(28); } homecoming mview; } private void loadarraydata() { seek { inputstream = contxt.getassets().open("data.csv"); bufferedreader reader = new bufferedreader(new inputstreamreader(is)); string line; while ((line = reader.readline()) != null) { string[] rowdata = line.split(","); datastates cur = new datastates(); cur.settitle(rowdata[2]); cur.setprice(rowdata[3]); cur.setdescription(rowdata[4]); this.add(cur); } } grab (ioexception e) { e.printstacktrace(); } } }

datastates

import android.net.uri; public class datastates { private string title; private string price; private string description; public string gettitle() { homecoming title; } public void settitle(string title) { this.title = title; } public string getprice() { homecoming price; } public void setprice(string price) { this.price = price; } public string getdescription() { homecoming description; } public void setdescription(string description) { this.description = description; } }

activity_main

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:orientation="vertical"> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemlistview" android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentstart="true" /> </linearlayout>

logcat info

10-07 21:43:31.241 25819-25819/tk.delacour.projectalpha d/dalvikvm﹕ late-enabling checkjni 10-07 21:43:31.241 25819-25819/tk.delacour.projectalpha d/dalvikvm﹕ seek disable coredump pid 25819 10-07 21:43:31.241 25819-25819/tk.delacour.projectalpha d/dalvikvm﹕ process 25819 nice name: tk.delacour.projectalpha 10-07 21:43:31.241 25819-25819/tk.delacour.projectalpha d/dalvikvm﹕ options: not specified 10-07 21:43:31.401 25819-25819/tk.delacour.projectalpha d/androidruntime﹕ shutting downwards vm 10-07 21:43:31.401 25819-25819/tk.delacour.projectalpha w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x42f05500) 10-07 21:43:31.411 25819-25819/tk.delacour.projectalpha e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{tk.delacour.projectalpha/tk.delacour.projectalpha.mainactivity}: java.lang.arrayindexoutofboundsexception: length=4; index=4 @ android.app.activitythread.performlaunchactivity(activitythread.java:2342) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2417) @ android.app.activitythread.access$600(activitythread.java:148) @ android.app.activitythread$h.handlemessage(activitythread.java:1355) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:150) @ android.app.activitythread.main(activitythread.java:5390) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) caused by: java.lang.arrayindexoutofboundsexception: length=4; index=4 @ tk.delacour.projectalpha.listviewadapter.loadarraydata(listviewadapter.java:63) @ tk.delacour.projectalpha.listviewadapter.<init>(listviewadapter.java:28) @ tk.delacour.projectalpha.mainactivity.oncreate(mainactivity.java:19) @ android.app.activity.performcreate(activity.java:5303) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2296)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2417)             at android.app.activitythread.access$600(activitythread.java:148)             at android.app.activitythread$h.handlemessage(activitythread.java:1355)             at android.os.handler.dispatchmessage(handler.java:99)             at android.os.looper.loop(looper.java:150)             at android.app.activitythread.main(activitythread.java:5390)             at java.lang.reflect.method.invokenative(native method)             at java.lang.reflect.method.invoke(method.java:525)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:553)             at dalvik.system.nativestart.main(native method)

ok programme fails @ point:

while ((line = reader.readline()) != null) { string[] rowdata = line.split(","); datastates cur = new datastates(); cur.settitle(rowdata[2]); cur.setprice(rowdata[3]); // there arrayindexoutofboundsexception cur.setdescription(rowdata[4]); this.add(cur); }

index begins @ 0 think right code

while ((line = reader.readline()) != null) { string[] rowdata = line.split(","); if(rowdata.length <4) continue; datastates cur = new datastates(); cur.settitle(rowdata[1]); cur.setprice(rowdata[2]); cur.setdescription(rowdata[3]); this.add(cur); }

java android listview csv android-listview

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -