我正在使用Parse开发实时信使应用程序,并希望在ListView中显示所有用户。所以我也想引用用户是在线还是离线,我尝试'put'方法添加名为“online”的列并输入相关信息。当它出错时,我自己添加了该列,但它仍然无效。这就是我在UserList类中尝试的内容:
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类:
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;
}
}
我使用SaveCallback尝试了saveInBackground,但它抛出了名为:
的异常引起:java.lang.IllegalArgumentException:在注册之前无法保存ParseUser。首先呼叫signUp
但是我比1小时前更困惑。我在另一个类中授权我的用户,似乎没有问题。
答案 0 :(得分:1)
这是Parse
的漏洞。可能还没有解决。 look at this和this。
而不是saveEventually()
使用saveInBackground()
和
你不能那样做
ParseUser user = new ParseUser();
如果你想要记录用户而不是像这样
ParseUser user = ParseUser.getCurrentUser();
在获得登录用户之前,您必须注册