Android Open Source - IMFApp Edit Friends Activity






From Project

Back to project page IMFApp.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project IMFApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.teamtreehouse.ribbit;
/*from ww w.ja  v  a 2s.c  om*/
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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;
import com.parse.SaveCallback;

public class EditFriendsActivity extends ListActivity {
  
  public static final String TAG = EditFriendsActivity.class.getSimpleName();
  
  protected List<ParseUser> mUsers;
  protected ParseRelation<ParseUser> mFriendsRelation;
  protected ParseUser mCurrentUser;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_edit_friends);
    // Show the Up button in the action bar.
    setupActionBar();
    
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    
    mCurrentUser = ParseUser.getCurrentUser();
    mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_REL);
    
    setProgressBarIndeterminate(true);
    
    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.orderByAscending(ParseConstants.KEY_USERNAME);
    query.setLimit(1000);
    query.findInBackground(new FindCallback<ParseUser>() {
      @Override
      public void done(List<ParseUser> users, ParseException e) {
        setProgressBarIndeterminate(false);
        
        if(e == null){
          // Success
          mUsers = users;
          String[] usernames = new String[mUsers.size()];
          int i = 0;
          for(ParseUser user : mUsers){
            usernames[i] = user.getUsername();
            i++;
          }
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(
              EditFriendsActivity.this, android.R.layout.simple_list_item_checked, usernames);
          setListAdapter(adapter);
          
          addFriendCheckmarks();
        }else{
          Log.e(TAG, e.getMessage());
          AlertDialog.Builder builder = new AlertDialog.Builder(EditFriendsActivity.this);
          builder.setMessage(R.string.friends_query_error_message)
              .setTitle(R.string.oops)
              .setPositiveButton(android.R.string.ok, null);
          AlertDialog dialog = builder.create();
          dialog.show();
        }
      }
    });
  }

  /**
   * Set up the {@link android.app.ActionBar}.
   */
  private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

  }
  
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    
    if(getListView().isItemChecked(position)){
      // add friend
      mFriendsRelation.add(mUsers.get(position));
    }else{
      // remove friend
      mFriendsRelation.remove(mUsers.get(position));
    }

    mCurrentUser.saveInBackground(new SaveCallback() {
      @Override
      public void done(ParseException e) {
        if(e != null){
          Log.e(TAG, e.getMessage());
        }
      }
    });
  }

  private void addFriendCheckmarks(){
    mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
      @Override
      public void done(List<ParseUser> friends, ParseException e) {
        if(e == null){
          for(int i = 0; i < mUsers.size(); i++){
            ParseUser user = mUsers.get(i);
            
            for(ParseUser friend : friends){
              if(friend.getObjectId().equals(user.getObjectId())){
                getListView().setItemChecked(i, true);
              }
            }
          }
        }else{
          Log.e(TAG, e.getMessage());
        }
      }
    });
  }
}




Java Source Code List

com.teamtreehouse.ribbit.EditFriendsActivity.java
com.teamtreehouse.ribbit.FriendsFragment.java
com.teamtreehouse.ribbit.InboxFragment.java
com.teamtreehouse.ribbit.LoginActivity.java
com.teamtreehouse.ribbit.MainActivity.java
com.teamtreehouse.ribbit.ParseConstants.java
com.teamtreehouse.ribbit.RibbitApplication.java
com.teamtreehouse.ribbit.SectionsPagerAdapter.java
com.teamtreehouse.ribbit.SignUpActivity.java