4 Replies - 690 Views - Last Post: 13 September 2012 - 06:46 PM

#1 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Error trying to read a text file and put it into xml textview

Posted 13 September 2012 - 05:23 PM

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
        InputStream iFile = getResources().openRawResource(R.raw.quizhelp);
        	TextView helpText = (TextView) findViewById(R.id.TextView_HelpText);
        	String strFile = inputStreamToString(iFile);
        	helpText.setText(strFile);
    }
}
        		



There is my code. The problem is with the inputStreamToString(iFile).It says that it is undefined. Im working my way through an android book and that is exactlly out of the book. I have looked around and tried a few different ways but cant seem to make it open the quizhelp file and display it in the TextView_QuizHelp. Any help is much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Error trying to read a text file and put it into xml textview

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 276
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: Error trying to read a text file and put it into xml textview

Posted 13 September 2012 - 06:03 PM

inputStreamToString is undefined because it is a user defined function not a build in function. To read in a text file from resources use the following code:
InputStream inputStream= getResources().openRawResource(R.raw.quizhelp);

InputStreamReader inputreader = new InputStreamReader(inputStream);
             BufferedReader buffreader = new BufferedReader(inputreader);
              String line;
              StringBuilder text = new StringBuilder();

              try {
                while (( line = buffreader.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                  }
            } catch (IOException e) {
               
            }
helpText.setText(text.toString());



Was This Post Helpful? 1
  • +
  • -

#3 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Re: Error trying to read a text file and put it into xml textview

Posted 13 September 2012 - 06:18 PM

That works with no errors but it still is not putting out my text file. Any other ideas on where i should look for problems? Im thinking maybe its an issue with the TextView in the XML?
Was This Post Helpful? 0
  • +
  • -

#4 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Re: Error trying to read a text file and put it into xml textview

Posted 13 September 2012 - 06:34 PM

  <RelativeLayout
         android:id="@+id/RelativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

     <TextView
         	android:id="@+id/TextView_HelpText"
         	android:layout_width="fill_parent"
         	android:layout_height="fill_parent"
         	></TextView>
        </RelativeLayout>



Thats just the part of the xml that pertains to the text view. I have a linear layout with the main title and an image then put the relative layout to go below that with the text view where the quizhelp file should go. I tried to run a test string on it as well with no success.
Was This Post Helpful? 0
  • +
  • -

#5 blankwall  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 12-July 12

Re: Error trying to read a text file and put it into xml textview

Posted 13 September 2012 - 06:46 PM

I figured it out there was a problem with the relative layout. Thank you so much for your help. I dont know how to rep but if you tell me Id be glad to give you one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1