-
-
Save anonymous/dda385e06e48a6a9e013 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.jackdh.tapchat; | |
import android.app.AlertDialog; | |
import android.support.v4.app.ListFragment; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v7.app.ActionBar; | |
import android.support.v4.app.Fragment; | |
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.os.Build; | |
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 java.util.List; | |
public class RecipientsActivity extends ActionBarActivity { | |
public MenuItem mSendMenuItem; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_recipients); | |
if (savedInstanceState == null) { | |
getSupportFragmentManager().beginTransaction() | |
.add(R.id.container, new recipientsFragment()) | |
.commit(); | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch(item.getItemId()) { | |
case R.id.action_send: | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@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_recipients, menu); | |
mSendMenuItem = menu.getItem(0); | |
return true; | |
} | |
public class recipientsFragment extends ListFragment { | |
public final String TAG = recipientsFragment.class.getSimpleName(); | |
protected List<ParseUser> mContacts; | |
protected ParseRelation<ParseUser> mFriendsRelation; | |
protected ParseUser mCurrentUser; | |
private ListView mListView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setHasOptionsMenu(true); | |
//TODO: Add spinner. | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_recipients, container, false); | |
return rootView; | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mCurrentUser = ParseUser.getCurrentUser(); | |
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION); | |
ParseQuery<ParseUser> query = mFriendsRelation.getQuery(); | |
query.addAscendingOrder(ParseConstants.KEY_USERNAME); | |
mFriendsRelation.getQuery().findInBackground( new FindCallback<ParseUser>() { | |
@Override | |
public void done(List<ParseUser> parseUsers, ParseException e) { | |
//TODO: Add progress spinner. | |
if (e == null) { | |
mContacts = parseUsers; | |
String[] usernames = new String[mContacts.size()]; | |
int i = 0; | |
for (ParseUser user : mContacts) { | |
usernames[i] = user.getUsername(); | |
i++; | |
} | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>( | |
getListView().getContext(), | |
android.R.layout.simple_list_item_checked, | |
usernames); | |
getListView().setAdapter(adapter); | |
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); | |
} else { | |
Log.e(TAG, e.getMessage()); | |
AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext()); | |
builder.setMessage(e.getMessage()) | |
.setTitle(R.string.error_title) | |
.setPositiveButton(android.R.string.ok, null); | |
AlertDialog dialog = builder.create(); | |
dialog.show(); | |
} | |
} | |
}); | |
} | |
@Override | |
public void onListItemClick(ListView l, View v, int position, long id) { | |
super.onListItemClick(l, v, position, id); | |
if (l.getCheckedItemCount() >0) { | |
mSendMenuItem.setVisible(true); | |
} else { | |
mSendMenuItem.setVisible(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment