具有带有复选标记的ListView。该代码侦听click,如果选中了该行,则会将列表/行的用户名添加到ParseUser的列(这是一个数组)。这是行不通的。它只会添加第一次点击,而其他任何点击都无济于事!欣赏是否有人可以看到原因。
Log.i显示单击该行,因此应将ParseUser的isFollowing列添加到该列。但事实并非如此。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
//To Avoid null pointer error
if (ParseUser.getCurrentUser().get("isFollowing") == null) {
List<String> emptyList = new ArrayList();
ParseUser.getCurrentUser().put("isFollowing", emptyList);
}
ListView listViewUsers = findViewById(R.id.listViewUsers);
listViewUsers.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
//users.add("TestUser1");
//users.add("TestUser2");
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, users );
listViewUsers.setAdapter(arrayAdapter);
listViewUsers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
if (checkedTextView.isChecked()) {
Log.i("onItemClick","row checked " + users.get(position));
ParseUser.getCurrentUser().getList("isFollowing").add(users.get(position));
ParseUser.getCurrentUser().saveInBackground();
} else {
Log.i("onItemClick", "row Not Checked " + users.get(position));
ParseUser.getCurrentUser().getList("isFollowing").remove(users.get(position));
ParseUser.getCurrentUser().saveInBackground();
}
}
});
users.clear();
ParseQuery<ParseUser> parseUserParseQuery = ParseUser.getQuery();
if (ParseUser.getCurrentUser() != null) {
username = ParseUser.getCurrentUser().getUsername();
Log.i("Current User: ", username );
parseUserParseQuery.whereNotEqualTo("username", username);
//parseUserParseQuery.addAscendingOrder("username");
parseUserParseQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseUser user : objects) {
users.add(user.getUsername());
}
arrayAdapter.notifyDataSetChanged();
}
}
}
});
}
}
}