简体   繁体   中英

retrieving string from parse.com and displaying it in TextView Android

I have uploaded data onto parse.com and i am trying to retrieve the data from parse.com and display it in a textview.

i have a parseconstants class:

public static final String TYPE_STRING = "string"; 
public static final String KEY_FILE_TYPE = "fileType";

i send the message and it uploads to parse fine

String fileName = "text.txt";
        String fileType = "text";

these 2 values are sent to parse.com as filename and filetype.

but when i get it back in inboxActivity here:

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

        ParseObject message = mUserBio.get(position);
        String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);
        ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);
        Uri fileUri = Uri.parse(file.getUrl());
        if (messageType.equals(ParseConstants.TYPE_STRING)){
            Intent intent = new Intent(getActivity(), ViewTextActivity.class);
            intent.setData(fileUri);
            startActivity(intent);
        }

it does not call on the text activity class and does not show the class.

in the TextViewController i try to display the text into the TextView like below:

mDisplay = (TextView)findViewById(R.id.bioDisplay);

    Uri textUri = getIntent().getData();
    File string = new File(textUri.toString());
    mDisplay.setText((CharSequence) string);

1) Why does the ViewtextActivity not show?

2) will the textview display the retrieved user bio?

Firstly, try to avoid calling constants like this:

ParseConstants.KEY_FILE_TYPE

Completely!

Rather import your class statically:

import static <your_package_name>.ParseConstants.*;
//now you can access your constants by calling it on its own:
String messageType = message.getString(KEY_FILE_TYPE);

EDIT

Your naming standards are not up to standard (An instance of File, should be called something to do with a File, and not "string")!! You are trying to display an object of type File within a TextView which won't work. Rather try this, instead of letting Android Studio cast it to a CharSequence:

mDisplay.setText(string.toString());

Thirdly, when calling ViewTextActivity, there are 3 ways to do this (as I am not sure if you are using a Fragment or not):

//if you are using a Fragment:
Intent intent = new Intent(getActivity(), ViewTextActivity.class);

//if you are using a FragmentActivity, you need to cast it:
Intent intent = new Intent((Your_Base_Activity) getActivity(), ViewTextActivity.class);

//if you are using a normal activity:
Intent intent = new Intent(Your_Activity_Name.this, ViewTextActivity.class);

As far as your TextView displaying data is concerned, your code does seem logically correct.

EDIT 2

From the ParseObject API, we see that getString will return the String value from that Key that is put in as the parameter ( See Here ).

According to you, you are checking if the return value is equal to the word "string". This doesn't make sense, as you are putting in the key value of "fileType".

My suggestion is check if the return is not null by using a log:

String messageType = message.getString(KEY_FILE_TYPE);
Log.e("MessageReturned: ", "Returned-" + messageType);

Or you can rethink, what the value that is supposed to equate to is supposed to be, as "string" doesn't make sense as far as I can see.

EDIT 3

Since you are saying that the value uploaded for the variable fileType is "text" should you not rather be doing this:

if (messageType.equals("text")){

EDIT 4

Your method of parsing information between the intents is obsolete. You need to try this:

if (messageType.equals(ParseConstants.TYPE_STRING)){
        Intent intent = new Intent(getActivity(), ViewTextActivity.class);
        intent.putExtra("fileUri", fileUri.toString());
        startActivity(intent);
}

Then in your other class, you access it like so:

Bundle extras = getIntent().getExtras(); 
Uri receivedFileUri = Uri.parse(extras.getString("fileUri"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM