Android Java - Reading in Text File of Integers, Storing Integers in Array, and Graphing Them -


so, code, trying read in .txt file contains integers, store integers array list via parseint(), , graphing points via graphview. have done debugging code , have noticed none of points being graphed , when test out code printing values array list via label, crashes app. unsure of going on, think on right track in process. here code below. *note: used textview labels tests determine if data read in (in case it's not). in advance!

mainactivity.java

package com.example.anjanarajagopal.graph;  import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.widget.textview;  import com.jjoe64.graphview.graphview; import com.jjoe64.graphview.series.datapoint; import com.jjoe64.graphview.series.linegraphseries;  import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list;  public class mainactivity extends actionbaractivity {      private static final string tag = mainactivity.class.getname();     private static final string filename = "data.txt";     private textview textview1;     private textview textview2;     private textview textview3;     private textview textview4;     private textview textview5;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          list<integer> data = new arraylist<integer>();         try         {             inputstream inputstream = openfileinput(filename);             if(inputstream != null){                 inputstreamreader streamreader = new inputstreamreader(inputstream);                 bufferedreader bufferedreader = new bufferedreader(streamreader);                 string line;                 while((line = bufferedreader.readline()) != null)                 {                     int = integer.parseint(line);                     data.add(i);                 }             }             inputstream.close();         }         catch(exception e)         {             system.err.format("exception occurred trying read '%s'.", filename);             e.printstacktrace();             //return null;         }         //for(int = 0; < data.size(); i++)         //{         //    int value = data.get(i);         //    system.out.print(value + " ");         //}         //textview1 = (textview)findviewbyid(r.id.textview1);         //textview2 = (textview)findviewbyid(r.id.textview2);         //textview3 = (textview)findviewbyid(r.id.textview3);         //textview4 = (textview)findviewbyid(r.id.textview4);         //textview5 = (textview)findviewbyid(r.id.textview5);         //textview1.settext(integer.tostring(data.get(0)));         //textview2.settext(integer.tostring(data.get(1)));         //textview3.settext(integer.tostring(data.get(2)));         //textview4.settext(integer.tostring(data.get(3)));         //textview5.settext(integer.tostring(data.get(4)));          graphview graph;         graph = (graphview) findviewbyid(r.id.graph);         for(int = 0; < data.size(); i++) {             linegraphseries<datapoint> series = new linegraphseries<datapoint>(new datapoint[]{                     new datapoint(i, data.get(i))             });             graph.addseries(series);         } } @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item);     }  } 

data.txt 142 149 146 142 152 165 178 188 171 169 179 155 129 117 125 155 204 251 270 265

activity_main.xml

<relativelayout 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">  <textview android:text="@string/hello_world" android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/textview5"     android:layout_below="@+id/textview4"     android:layout_alignleft="@+id/textview3"     android:layout_alignstart="@+id/textview3" /> <com.jjoe64.graphview.graphview     android:layout_width="match_parent"     android:layout_height="200dip"     android:id="@+id/graph"     android:layout_alignparenttop="true"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" />  <textview     android:text="@string/hello_world"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/textview2"     android:layout_centervertical="true"     android:layout_centerhorizontal="true" />  <textview     android:text="@string/hello_world"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/textview3"     android:layout_below="@+id/textview2"     android:layout_alignright="@+id/graph"     android:layout_alignend="@+id/graph" />  <textview     android:text="@string/hello_world"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/textview4"     android:layout_below="@+id/textview3"     android:layout_alignleft="@+id/textview2"     android:layout_alignstart="@+id/textview2" />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="hello world!"     android:id="@+id/textview1"     android:layout_below="@+id/textview5"     android:layout_alignright="@+id/textview4"     android:layout_alignend="@+id/textview4" /> </relativelayout> 

in particular case should put data.txt in src/main/assets folder. instead of line

inputstream inputstream = openfileinput(filename); 

use following line

inputstream inputstream = getassets().open(filename); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -