io - read file from assets android -
i'm trying build card viewer in android trading card game. plan use arraylist populate cards database. read in values file , displayed via listview. listview code , arraylist code fine (after testing without using file input). problem occurs when attempt read in file. i'm @ loss of do. when run program this, crashes. when remove "parseint" method (because i'm reading in integer file), runs, none of data populated. wrong io code?
import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import java.util.arraylist; import java.io.*; import android.widget.arrayadapter; import android.widget.listview; public class deckbuilderactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { intent intent = getintent(); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_deck_builder); //populate database************************************************************************ arraylist<card> database = new arraylist<card>(); //create new arraylist of datatype card //value holders string cardname, expansion, cardnum, unittype, unitclan, unitrace, trigger, unitskill, cardlore, imageid; int unitgrade, unitpower, unitshield; //exception handling try{ string myfile = "cardmaindatabase.txt"; inputstream inputreader = getassets().open(myfile); bufferedreader reader = new bufferedreader(new inputstreamreader(inputreader)); //open file //begin populating loop int = 0; //incrementer while(reader.readline() != null) //while not end of file { //assign values placeholders //add new card , take parameters database.add(new card()); database.get(i).setcardname(reader.readline()); database.get(i).setexpansion(reader.readline()); database.get(i).setcardnum(reader.readline()); database.get(i).setunitgrade(integer.parseint(reader.readline())); } } catch (ioexception e) { e.printstacktrace(); } //****************************************************************************************** //populate arraylist arrayadapter<card> adapter = new arrayadapter(this, android.r.layout.simple_list_item_1, database); listview lv = (listview) findviewbyid(r.id.card_database_listview); lv.setadapter(adapter); }
stringbuffer stringbuffer = new stringbuffer(); try { inputstream = mcontext.getresources().getassets().open("your-file-path"); bufferedreader br = new bufferedreader(new inputstreamreader(is, "utf-8")); string line; while ((line = br.readline()) != null) { //play each line here } }...
try code data assets.
or if don't want clear code. try modify code this. think problem in loop.
string line = null; while((line = reader.readline()) != null) //while not end of file { //play each line here }
but collecting/storing data unstable , hard maintain. i'll suggest use json. store data in asset,so able collect , store data fast possible.
Comments
Post a Comment