Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Self-Destructing Message Android App Sending Messages Selecting Recipients

adnanalvee2
adnanalvee2
6,862 Points

Recipents Fragment

I tried to do this one in fragment, and everything worked but I am not seeing the Progress Bar, Im sort of confused in where to put that piece of code, As Bens OnCreate method is different from mine.

I tried putting it in my onCreate method, I don't get any errors and also don't see the Progress Bar either :P

I tried to place it OnResume method right after the Super Call, still no use. Any suggestions?

Here is my RecipentsActivity.java file.

package com.dipnsa.dipdip;

import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseRelation;
import com.parse.ParseUser;

public class RecipentsActivity extends ListActivity {


    public static final String TAG = RecipentsActivity.class.getSimpleName();

    protected List<ParseUser> mFriends;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;




    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_recipents, container,
                false);

        return rootView;
    }




    public void onResume() {
        super.onResume();
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        mCurrentUser = ParseUser.getCurrentUser();
        mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);

        setProgressBarVisibility(true);

        ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
        query.addAscendingOrder(ParseConstants.KEY_USERNAME);
        mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> friends, ParseException e) {
                setProgressBarVisibility(false);

                    if (e == null)  {
                    mFriends = friends;                 
                    String[] usernames = new String[mFriends.size()];
                    int i = 0;
                    for(ParseUser user : mFriends) {
                        usernames[i] = user.getUsername();
                        i++;
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                            getListView().getContext(),
                            android.R.layout.simple_list_item_checked,
                            usernames);
                    setListAdapter(adapter);
                    }

                else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(RecipentsActivity.this);
                    builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            }



        });

    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

2 Answers

william parrish
william parrish
13,774 Points

Try putting the progress bar visibility line inside mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() { // put it here// @Override .... }

If you think about it, you want the progress indicator to be visible while the above background task is executing, so make it a part of that.

You should try using the Android Studio version to create an Activity with a Fragment, then extend the fragment for a ListFragment and do all the coding in there. It should naturally keep the ActionBar and allow you to use the automated list funcionality android provides.