Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*7.1
- We can upload cat videos and pictures of food until
- the cows come home, but nobody can view them yet.
- So let's fix that.
- We're going to see how to retrieve data from our back end on parse.
- And then we'll see how to view photos and videos from the inbox.
- */
- // We'll want the inbox refreshed every time it's displayed.
- // So we'll add code inside the onResume method in FriendsFragment.java
- //We are going to query the message class/table but we only want messages that match our User ID //We only want to get messages sent to us
- // Parse Query, and what it is:
- // In many cases, getInBackground isn't powerful enough to specify
- which objects you want to retrieve. The ParseQuery offers different ways to retrieve a list of objects rather than just a single object. https://parse.com/docs/android/guide#queries
- protected List<ParseObject> mMessages; //[*M*]
- /7.1 We'll want the inbox refreshed every time it's displayed.
- // So we'll add code inside the onResume method.
- @Override
- public void onResume() {
- //We are going to query the message class/table but we only want messages that match our User ID //We only want to get messages sent to us
- super.onResume();
- getActivity().setProgressBarIndeterminateVisibility(true);
- ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
- //where clause: (if you are familiar with SQL, you'll know this), it desognates how to search through a collection
- // we want to find all messages, WHERE(.whereEqualTo()) our current user ID is of of the recipients.
- query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId()); //ParseUser.getCurrentUser().getObjectId() = gets the current user's ID
- query.addDescendingOrder(ParseConstants.KEY_CREATED_AT); //make the most recent messages at the top
- query.findInBackground(new FindCallback<ParseObject>() { //runs the query in the background
- @Override
- public void done(List<ParseObject> messages, ParseException e) {
- getActivity().setProgressBarIndeterminate(false); //dismiss the progressbar indicator
- if (e == null) { //if exceptions == null:
- //We found messages!
- mMessages = messages; //[*M*]
- // copied over from FriendsFragment.java
- String[] usernames = new String[mMessages.size()];
- int i = 0;
- for (ParseObject message : mMessages) {
- usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
- i++;
- }
- //ArrayAdapter to adapt to the list*
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(
- getListView().getContext(),
- android.R.layout.simple_list_item_1,
- usernames);
- setListAdapter(adapter);
- }
- }
- });
- }
- // 7.3 Creating a custom Layout for messages
- Create a custom layout so that the messages show "Photo" icon or "Video" icon next to the messages.
- // Step 1. Import the icon images into drawable
- // Step 2. Create custom list view adapter.
- -adapter has two parts:
- 1. First, we need to define a custom layout that will be used for each item in the list.
- 2. Then we need to create a custom class that adapts one message of our Parse objects into the layout.
- New >Layout file > RelativeLayout, name it: message_item
- //and then change the layout paddings and stuff a little to make it look good. //Simple
- // 7.4 Create a custom list adapter
- package com.joinedit.ribbitpractice;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.parse.ParseObject;
- import java.util.List;
- public class MessageAdapter extends ArrayAdapter<ParseObject> {
- protected Context mContext;
- protected List<ParseObject> mMessages;
- //7.4 create custom layout for messages
- public MessageAdapter(Context context, List<ParseObject> messages) {
- super(context, R.layout.message_item);
- mContext = context;
- mMessages = messages;
- }
- //7.4 Override getView() in order to use a custom list adapter.
- // for adapters for fragments, the adapter called a certain method to get an appropriate fragment.
- // And then it went ahead and adapted it and put it in the view.
- //This adapter, which is going to be attached to a list view,
- // is going to call a method, called getView,
- // and it's going to create the view, inflate it into a layout and then attach it into the list view.
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //7.4 we want to do getView as efficiently as possible because the more efficient we are in this method, the faster it will perform
- ViewHolder holder;
- //RecyclerView (recycle views if it already exists.)
- if (convertView == null) { //if the view holder does not exist: create new holders
- convertView = LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
- holder = new ViewHolder();
- holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon); //if you do (ImageView)findViewById(R.id.messageIcon) it will be RED error. need "convertView." in front
- //^ find view by ID is an activity method.
- //But we can call it from the convert view because in this instance, we want to get it for the layout for this view.
- //Because we're not attached to the activity yet.
- //do the same thing for TextView holder:
- holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
- } else { // else if it already exits: reuse the old holders
- holder = (ViewHolder)convertView.getTag(); //.getTag() method gets the holder that was already created
- }
- //7.4 Now that we have the view, get the ParseObject that correspond to each item on the list:
- ParseObject message = mMessages.get(position); //get the positon of the message
- //7.4 change icon of the message in layout based on message type
- if (message.getString(ParseConstants.KEY_FILE_TYPE).equals(ParseConstants.TYPE_IMAGE)){ //if image file type ".equals" image then:
- holder.iconImageView.setImageResource(R.drawable.ic_action_picture); //set message Icon
- } else { //else set it to image icon for video:
- holder.iconImageView.setImageResource(R.drawable.ic_action_play_over_video);
- }
- holder.nameLabel.setText(message.getString(ParseConstants.KEY_SENDER_NAME)); //set message sender name
- return convertView;
- }
- //7.4 Create a ViewHolder class to use viewHolder (android convention)
- private static class ViewHolder {
- //7.4 a very simple class that just includes the data that is going to be displayed in the custom layout.
- ImageView iconImageView;
- TextView nameLabel;
- }
- }
- //--------------------------------------------------------------------------------------
- // 7.6 View the messages (picture or videos)
- // Present a new activity who's entire layout is a single image view to show the image when you click on a message(item) in the list
- // And n the spirit of having messages that self destruct, we'll add a timer that finishes the activity after ten seconds.
- // Step 1. New > Activity > Blank Activity
- Name it ViewImageActivity
- package com.joinedit.ribbitpractice;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ImageView;
- public class ViewImageActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_view_image);
- ImageView imageView = (ImageView)findViewById(R.id.imageView); //set it to the imageView in activity_view_image.xml
- Uri imageUri = getIntent().getData(); //get the Uri that was passed by the intent in InboxFragment: intent.setData(fileUri);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_view_image, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- // 7.7 Using Picasso to View Images from the Web
- // can't use the android sdk's imageView method to view images from the web because that must be done asynchronously.
- // So use Picasso
- // Essentially use this:
- Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
- //simple.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_view_image);
- ImageView imageView = (ImageView)findViewById(R.id.imageView); //set it to the imageView in activity_view_image.xml
- Uri imageUri = getIntent().getData(); //get the Uri that was passed by the intent in InboxFragment: intent.setData(fileUri);
- Picasso.with(this).load(imageUri.toString()).into(imageView); // using Picasso is easy, they have an easy way of passing in the context: "Picasso.with(this)"
- //^this line basically says, "Hey Picasso, with the current context, load this image (uri) into this imageView.
- }
- //7.8 Viewing Videos
- // A video intent that sends the user to their video player they have on their phone to view the video
- // super easy to set up
- // In InboxFragment.java under
- // public void onListItemClick
- //7.7 view the video
- Intent intent = new Intent(Intent.ACTION_VIEW, fileUri); //view the file uri
- intent.setDataAndType(fileUri, "video/*");
- startActivity(intent);
Add Comment
Please, Sign In to add comment