get last arraylist position before streak gets interrupted - android

https://www.google.com/search?q=message+image+on+last&sxsrf=ALeKk01qNrLQ61yRXCMSPRxyNaoI0MF_kA:1605295893515&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjj2veboYDtAhWeIbcAHebnB4QQ_AUoAXoECAwQAw&biw=1366&bih=657#imgrc=wzgnznL_g7kUsM
I want to show profile image like shown in above link only at last message of every consecutive messages from same person. Please help me with this.
I have tried many ways but I don't know to hide visibility of top images
if (position > 0) {
if (messageList.get(i).getFrom.equalsIgnoreCase(messagesList.get(position - 1).getFrom()))
{
viewHolder.mProfileImage.setVisibility(View.INVISIBLE);
}
else
{
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
}
else
{
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
I have used above code and get this result which is shown in this http://i.stack.imgur.com/92SUb.jpg link. With this code I am able to hide all the images other than the first one image. can you please show me the way to hide top message image and not the last one of streak.

I have to assume few things in my answer.
i.e.
messageList is the list of all messages. not the list of only consecutive/streak messages.
you need to show profile images for both sender and receiver.
And you want to only show the profileImage of last consecutive message.
So, here what you should do.
int length = messageList.lenght; //getTotalLenght;
String fromCurrent = messageList[position].getFrom(); //getFrom of current message being painted or shown
String fromNext = fromCurrent; //next message is set to current by default, to avoid error
if(length > postition+1) {
fromNext = messageList[position+1].getFrom(); //if there is next message then get its sender but if it is the last message then nextSender and currentSender are same as I set its default value to the currentSender.
}
console.log("TAG", "FromNext: " + fromNext);
console.log("TAG", "FromCurrent: " + fromCurrent);
if(!fromCurrent.equals(fromNext)) { //checking if last message or not
viewHolder.mProfileImage.setVisibility(View.VISIBLE);
}
else {
viewHolder.mProfileImage.setVisibility(View.INVISIBLE);
}

Related

Kotlin shuffle a list and validate list value with user value

I have a list that holds string values as directions, for each time is started is shuffle the list
val directions = mutableListOf("left","right","up","down")
directions.shuffle()
I am running a continuous loop to check if the percentage value has grown to a certain point before breaking the loop
if(progress != 100){
//continue until progress is 100
}
I send the instructions to the user and if they match the progress value increases
when(directions){
"left" -> {
// print direction to the user then check if followed
if(checkLeftDirection() == "left"){
// increment progress then remove left from the list
directions.remove("left")
progress +=10
}
// other directions
else -> {
//the loop always ends here
}
}
I want to send a list of random directions to the user and for each check if the user was correct and if the user is correct for a given direction then remove that direction from the list and send another random direction to the user.
what happens is the loop runs and does nothing and just goes to the else statement, it loops all directions then ends at the else statement,
UPDATE
here is the loop
if (progress != 100) {
for (item in directions) {
when(item){
"left" -> {
// print direction to the user then check if followed
if(checkLeftDirection() == item){
// increment progress then remove left from the list
directions.remove(item)
progress +=10
}
// other directions
else -> {
//the loop always ends here
}
}
}

How to generate random numbers so that every number only occurs once?

I want to generate random numbers when clicking a button and every random number only occurs once. Now, I have an array in which I store all the generated numbers so I can check whether or not this number has been previously generated. However, when this is the case I want it to generate another number which has not been previously generated. I have been struggling but can't find the right code for this.
var arraylist = ArrayList<Int>()
nextnumberbutton.setOnClickListener {
val rand = java.util.Random().nextInt(75)
if (arraylist.contains(rand)) {
kotlin.run { nextnumberbutton }
} else {
numbertextview.text = rand.toString()
}
arraylist.add(rand)
}
run { nextnumberbutton } doesn't do anything. It's a lambda that will simply return Unit.
Supposing you did call the click listener again when a repeated number is found, you would still be adding the number to the list again, since you don't return from the function early. So you would end up with duplicates in your list.
Your strategy could be done with a while loop instead of calling the whole function again when a duplicate is picked. e.g. you could use while (rand !in arrayList) rand = Random.nextInt(75). However, this has the potential to take longer and longer to pick the number as you approach the limit of values because it is simply guessing numbers and has to try again if the number is already picked.
A better strategy would be to start with a set of all the numbers and remove from this set as you go. You also need to handle the case of when all numbers are picked, perhaps by showing a message to the user.
val unpickedNumbers = (0..75).toMutableSet()
nextnumberbutton.setOnClickListener {
if (unpickedNumbers.isEmpty()) {
Toast.makeText(context, "All numbers already picked", Toast.LENGTH_LONG).show()
return
}
val rand = unpickedNumbers.random()
numbertextview.text = rand.toString()
unpickedNumbers.remove(rand)
}

Parse: +1 to a number column for rating

My Android app has a very simple love it or hate it rating system. I am using Parse for the ratings.
All is fine in terms of querying Parse for number of ratings and also putting new ratings into parse.
The problem I'm having is:
I have not found a way on Parse to +1 to a number column, so what I have been doing is getting the current value of the rating, +1 to that number and then using Parse's Put method to write it back.
This seems ok but if 2 users open the app 1 second after the other they both see the rating is 8, then both click +1, it will get the value of the rating (8) for user A, +1 to 8 = 9 and then write 9 back.
1 second later user B does the same thing, they still have the value of 8 stored on their device so theirs will also be 8 +1 = 9, where it should have been 10.
So my question is: Is there a way to +1 to a number column in Parse?
If not I'll add the getRatings method before a user rates so it should get the latest value but was just hoping to reduce bandwidth.
Thanks.
Adam
public void getRatings() {
//query to get love it/hate it from Parse
//we are only quering to get the first in background because we know there is only
//one we want
ParseQuery<ParseObject> query = ParseQuery.getQuery("ratings");
query.whereEqualTo("Id", id);
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException error) {
if (object != null) {
//if the show has been rated before
loveIt = object.getInt("loveIt");
hateIt = object.getInt("hateIt");
retreivedObject = object;
parseId = object.getObjectId();
//hide progress bar
mProgressBar.setVisibility(View.INVISIBLE);
}
else
{
//if the show has not been rated before
loveIt=0;
hateIt=0;
//hide progress bar
mProgressBar.setVisibility(View.INVISIBLE);
}
}
});
}
You need to use a counter. See official documentation below.
https://www.parse.com/docs/android_guide#objects-updating

Trying to get an Android jabber app to check for a string in new messages and return a toast message

I have been able to create the if statement that checks for the string and it returns the toast message that i created but it keeps showing the toast message every time i open the chat. even if the most recent message doesn't contain the string I am looking for so i am assume it isn't checking to see if it is the last message received and it doesn't check to see if it is unread. the code is below. the reason i am trying to do this is because my parents share a facebook account and i want an easy way to display if the message is signed mom or dad. the code below only has the check for mom once it works i will be adding the check for dad signature. I am using the open source message client Xabber. Thank you for help.
public void setVisibleChat(String account, String user) {
final boolean remove = !AccountManager.getInstance()
.getArchiveMode(account).saveLocally();
AbstractChat chat = getChat(account, user);
if (chat == null)
chat = createChat(account, user);
else {
// Mark messages as read and them delete from db if necessary.
final ArrayList<MessageItem> messageItems = new ArrayList<MessageItem>();
for (MessageItem messageItem : chat.getMessages()) {
if (!messageItem.isRead()) {
messageItem.markAsRead();
messageItems.add(messageItem);
}
if (chat.getLastText().contains("Mom") && (!messageItem.isRead()));{
Toast.makeText(Application.getInstance(), "Message from Mom!", Toast.LENGTH_SHORT).show();
}
}
Application.getInstance().runInBackground(new Runnable() {
#Override
public void run() {
Collection<Long> ids = getMessageIds(messageItems, remove);
if (remove)
MessageTable.getInstance().removeMessages(ids);
else
MessageTable.getInstance().markAsRead(ids);
}
});
}
visibleChat = chat;
}
You've got an extra semi-colon here
if (chat.getLastText().contains("Mom") && (!messageItem.isRead())); <------
So your next block of code containing the Toast show statement will always be executed.
Remove the semi-colon

OnRealTimeMessageSent callback not being fired reliably?

With Google Play Game Services, I'm trying to implement a CallBack such that, if there is an issue with sending a message, then I need to resolve it (as each player "passes" their bid to the next player, and all other players need to see what the player that passed bid)
I thought I would try and use the following to instantiate a RealTimeReliableMessageSentListener for that round of messages, so that I can tell if the message was sent and received by everyone:
(I add the tokenID returned by the call to an ArrayList, and then check off remove each tokenID as it comes back in to track when all messages from this round are received)
#Override
public void sendReadyToPlay() {
dLog("Sending ReadyToPlay");
// Broadcast that I'm ready, and see that they are ready
if (!mMultiplayer){
return; // playing against computer
}
// First byte in message indicates whether it's a final score or not
mMsgBuf[0] = (byte) ('R');
readyToPlayTokens.clear();
// Send to every other participant.
for (Participant p : mParticipants) {
dLog("Participant:" + p.getParticipantId());
if (p.getParticipantId().equals(mMyId)) {
continue;}
if (p.getStatus() != Participant.STATUS_JOINED){
continue;
}
readyToPlayTokens.add(mHelper.getGamesClient().sendReliableRealTimeMessage(new RealTimeReliableMessageSentListener() {
#Override
public void onRealTimeMessageSent(int statusCode, int tokenId, String recipientParticipantId){
dLog("onRealTimeMessageSent number two and size is: " + readyToPlayTokens.size());
if(readyToPlayTokens.contains(tokenId)){
readyToPlayTokens.remove(tokenId);
}
dLog("onRealTimeMessageSent number two and size is: " + readyToPlayTokens.size());
if (statusCode != GamesClient.STATUS_OK) {
mGHInterface.onRealTimeMessageReceived("RTPProblem:" + recipientParticipantId);
} else if (readyToPlayTokens.size() == 0) {
mGHInterface.beginRound();
}
}
}, mMsgBuf, mRoomId, p.getParticipantId()));
dLog("sent to:" + p.getParticipantId());
}
}
I can see the messages coming in almost every time from one device to another, so I can see that the messages are going through, BUT, the RealTimeReliableMessageSent listener is only being fired about 50-60 percent of the time, which isn't very reliable! :)
Can anyone see what I might be doing wrong to keep the listener from firing reliably?
may be it happens because you are using anonymous inner class, try to use in different way like implementing your activity RealTimeReliableMessageSentListener

Categories

Resources