Search code examples
javaandroidparse-platformtextview

Receive text file from parse.com and place in textview


I am trying to get the text from a text file in Parse and convert it to String and place the text in a textview. I tried doing what someone suggested while researching by doing it the same way as an image (AppetizerRecipe.java)

As you can see from the images, i have a column "recipe" that consists of text files. The logcat shows that the file is retrieved from parse and when I click on it, it shows me my file. I can't seem to get the text onto my textview.

onListItemClick:

@Override
protected void onListItemClick(ListView l, View v, int position, long
        id) {
    super.onListItemClick(l, v, position, id);

    ParseObject po = mAppetizers.get(position);

    ParseFile recipes = po.getParseFile("recipe");


    // TODO: Get fields from Parse
    String title = po.getString(AppetizerRecipe.ARG_NAME);
    String time = po.getString(AppetizerRecipe.ARG_TIME);
    String ID = po.getObjectId().toString();
        Log.d("Appetizer", "object: " + ID);

    Intent intent = new Intent(Appetizer.this, AppetizerRecipe.class);

    // TODO: Put extras
    intent.putExtra(AppetizerRecipe.ARG_NAME, title);
    intent.putExtra(AppetizerRecipe.ARG_TIME, time);
    intent.putExtra("ID", ID);
        Log.d("Appetizer", "object: " + ID);


    startActivity(intent); // TODO:  In other activity, get these extras
}

AppetizerRecipe.java

ParseQuery<ParseObject> gettext = new ParseQuery<>("Appetizers"); 
    gettext.addAscendingOrder("appetizer"); 
    gettext.whereEqualTo("ID", ID); 
    gettext.getInBackground(ID, new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject textobject, ParseException e) {
            if (e == null) {
                // success
                final ParseFile textObject = (ParseFile) textobject.get("recipe");
                textObject.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                        if (e == null) {
                            // use data for something

                        } else {

                        }
                    }
                });

            } else {
                // fail
                Log.d("test", "Error Message..." + e.getMessage());
            }
        }
    });

parse.com:

parse.com

Logcat:

Logcat

Text file from parse.com:

Text file from parse.com


Solution

  • To get a String from a byte[]:

    //your byte[] being data
    String str = new String(data);
    

    and then set the String on your TexView like:

    textView.setText(str);