1

I'm developing real-time messenger application with Parse and want to display all of users in ListView. So I also want to reference if user is online or offline, I tried 'put' method to add column named "online" and put information about it. When it went wrong, I added that column myself, but it still did not work. Here it's what I tried in UserList class at all:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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

import java.util.ArrayList;
import java.util.List;

public class UserList extends AppCompatActivity {
     public static ArrayList<ParseUser> userList;
     public static String TAG = "UserList";
     public static ParseUser user = new ParseUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_list);
    updateUserStatus(true);

}
@Override
protected void onDestroy() {
    super.onDestroy();
    updateUserStatus(false);
}
@Override
protected void onResume() {
    super.onResume();
    loadUserList();
}
private void updateUserStatus(boolean isOnline) {
    user.put("online", isOnline);
    user.saveEventually();
    //System.out.println("getBoolean's result : " + user.getBoolean("online"));
}
private void loadUserList() {
    ParseUser.getQuery().whereNotEqualTo("username", user.getUsername()).findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> objects, ParseException e) {
            if (objects != null) {
                if (objects.size() == 0) System.out.println("No user found");
                userList = new ArrayList<>(objects);
                ListView list = (ListView) findViewById(R.id.userList);
                list.setAdapter(new UserAdapter(UserList.this));
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        startActivity(new Intent(UserList.this, MainActivity.class));
                        finish();
                    }
                });
            }
            else {
                System.out.println("exception detected while loading user list");
                e.printStackTrace();
            }
        }
    });
  }
}

UserAdapter class:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.parse.ParseUser;

public class UserAdapter extends BaseAdapter {
    public static String TAG = "UserAdapter";
    public TextView labelname;
    LayoutInflater layoutInflater;

public UserAdapter(Context context){
    layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return UserList.userList.size();
}
@Override
public ParseUser getItem(int index) {
    return UserList.userList.get(index);
}
@Override
public long getItemId(int index) {
    return index;
}
@Override
public View getView(int pos, View v, ViewGroup group) {
       ParseUser c = getItem(pos);
        if (v == null) {
            v = layoutInflater.inflate(R.layout.chat_item, null);
        }
        labelname = (TextView) v;
        labelname.setText(c.getUsername());
        labelname.setCompoundDrawablesWithIntrinsicBounds(c.getBoolean("online") ? R.drawable.ic_online
                : R.drawable.ic_offline, 0, R.drawable.arrow, 0);

    return v;
   }
}

I tried saveInBackground with SaveCallback, but it throws exception named:

Caused by: java.lang.IllegalArgumentException: Cannot save a ParseUser until it has been signed up. Call signUp first

But I'm puzzled more than I was 1 hour ago. I authorize my user in another class and it seems not to be problem.

1 Answer 1

1

This was open bug of Parse. might be possible that not solved yet. look at this and this too.

instead of saveEventually() use saveInBackground()

and

you can't do like that

ParseUser user = new ParseUser();

if you want logged user than get like this

ParseUser user = ParseUser.getCurrentUser();

and before getting logged user you must do sign up

Sign up to request clarification or add additional context in comments.

5 Comments

So, what should I do now? I looked at messenger's demo based on Parse and it was using the 'put' method,it worked fine. I'm pretty puzzled.
you can use saveInBackground()
You mean "user.saveInBackground();" after calling 'put' method,yes? I'll try it.
It isn't working. I tried it and with SaveCallback,too. But it throws exception and I will edit my post to show you that.
God, it works! Thank you. I'll comment if something goes wrong about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.