mercredi 1 avril 2015

How to pass a string from a list in one activity to another

What I would like to do is convert the list of user into a string and on click pass the selected string to the next activity. I tried this on a messaging feature I have used on the app and it has worked fine. Now I am attempting to work with a gallery feature. What this section is supposed to do is find all the users in parse, convert them to string and display them on the list view. On click it is supposed to collect the string on the list and use it in the gallery view activity to display all the images where the username is equal the one selected from the list.


ListUserActivity


List view of all the usernames saved in the database.



//display clickable a list of all users
private void setConversationsList() {
currentUserId = ParseUser.getCurrentUser().getObjectId();
names = new ArrayList<String>();

ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("objectId", currentUserId);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> userList, com.parse.ParseException e) {
if (e == null) {
for (int i=0; i<userList.size(); i++) {
names.add(userList.get(i).getUsername().toString());
}

usersListView = (ListView)findViewById(R.id.usersListView);
namesArrayAdapter =
new ArrayAdapter<String>(getApplicationContext(),
R.layout.user_list_item, names);
usersListView.setAdapter(namesArrayAdapter);

usersListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
openConversation(names, i);
}
});

} else {
Toast.makeText(getApplicationContext(),
"Error loading user list",
Toast.LENGTH_LONG).show();
}
}
});
}


What happens on clicking the name on the list view.



public void openConversation(ArrayList<String> names, int pos) {
ParseQuery<ParseUser> query = ParseUser.getQuery();

query.whereEqualTo("username", names.get(pos));
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> user, com.parse.ParseException e) {
if (e == null) {
// Intent intent = new Intent(getApplicationContext(), GridActivity.class);
Intent intent = new Intent(ListUsersActivity.this, GridActivity.class);
intent.putExtra("RECIPIENT_ID", user.get(0).getObjectId());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Error finding that user",
Toast.LENGTH_SHORT).show();
}
}
});
}


GalleryActivity


I've tried to convert the RECIPIENT_ID to a string variable called recipientId and add it in the line query.whereEqualTo("username", recipientId); but it keeps creating a message telling me it is NULL.



@Override
protected Void doInBackground(Void... params) {
// Create the array

Intent intent = getIntent();
recipientId = intent.getStringExtra("RECIPIENT_ID");
currentUserId = ParseUser.getCurrentUser().getObjectId();


phonearraylist = new ArrayList<PhoneList>();
try {

ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("ImageUpload");


usernametxt = usernameimgshow.getText().toString();
//query.whereEqualTo("username", userstr);
query.whereEqualTo("username", recipientId);
query.orderByAscending("createdAt");
ob = query.find();
for (ParseObject country : ob) {
ParseFile image = (ParseFile) country.get("ImageFile");
PhoneList map = new PhoneList();
map.setPhone(image.getUrl());
phonearraylist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

Aucun commentaire:

Enregistrer un commentaire