Reading from a file in res/raw

Put your problem here if it does not fit any of the other categories.

Reading from a file in res/raw

Postby willis127 » Wed Nov 11, 2009 8:43 pm

Ok I'm trying to read a file line by line and split it based on a bar into 3 seperate groups and assign each one to an array. My problem is it doesnt work and I can't for the life of my figure out why.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public static int ReadData(Context context){
  2.         InputStream is;
  3. try {
  4.     is = context.getResources().openRawResource(R.raw.asg);
  5.     InputStreamReader isr = new InputStreamReader(is);
  6.     BufferedReader br = new BufferedReader(isr);
  7.  
  8. String[] strLine;
  9. Integer[] uid = null;
  10. String[] question = null;
  11. String[] answer = null;
  12. String[] temp = null;
  13. strLine = new String[3];
  14.  
  15. for (int j = 0; j < strLine.length; j++){
  16. strLine[j] = br.readLine();
  17. temp = strLine[j].split("\|");
  18. uid[j] = Integer.parseInt(temp[0]);
  19. question[j] = temp[1].toString();
  20. answer[j] = temp[2].toString();
  21.   }
  22. return uid[1];
  23.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Also when I call this method from MainActivity I get an error at ReadData() that says


method ReadData cannot be applied to given types.
required: android.content.Context


This is my MainActivity Listener to use the above method.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private OnClickListener readListener = new OnClickListener()
  2. {
  3.         public void onClick(View v)
  4.         {
  5.             flashcard.ReadData();
  6.             Integer amountOfLines = uid[2];
  7.             final TextView xlines = (TextView) findViewById(R.id.xtext);
  8.             xlines.setText(amountOfLines.toString());
  9.         }
  10. };
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Thanks in advance
willis127
Freshman
Freshman
 
Posts: 9
Joined: Wed Nov 11, 2009 8:03 pm

Top

Postby Oleg » Sat Nov 14, 2009 2:54 pm

Hi.
You need to add Context to ReadData() method:
flashcard.ReadData(this);
or
flashcard.ReadData(MainActivity.this);
Oleg
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Wed Nov 28, 2007 1:49 pm
Location: Ukraine

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 16 guests