我在内部存储中保存了一个String数组,并且我使用它来从Parse.com检索特定用户的名称。这是代码:
SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
final ListView idList;
idList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, new ArrayList<String>());
idList.setAdapter(str);
for(String userIds : myStrings) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.getInBackground(userIds, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
str.add(name);
}
});
}
idList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String names = str.getItem(position);
Toast.makeText(UserListForHistory.this, names, Toast.LENGTH_SHORT).show();
}
});
有没有办法对它进行配置,这样当我点击一个项目时,它会显示保存在&#34; myString&#34;中的用户ID。阵列?因为我想显示具有特定名称的列表,但是在点击时我希望得到特定用户objectid。 我知道如何才能完成的唯一方法是将String UserIds直接保存到适配器,但是它会在列表上显示ID而不是名称。
//编辑解决方案
SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
final List<String> users = new ArrayList<>();
final ListView idList;
idList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, new ArrayList<String>());
idList.setAdapter(str);
for(final String userIds : myStrings) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.getInBackground(userIds, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
str.add(name);
users.add(userIds);
}
});
}
idList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String user = users.get(position);
Toast.makeText(UserListForHistory.this, user, Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:1)
如果我理解,这是一种方法:
SharedPreferences settings = getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE);
Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>());
List<ParseObject> users = new ArrayList<>();
final ListView idList;
idList = (ListView) findViewById(android.R.id.list);
final ArrayAdapter<String> str = new ArrayAdapter<>(getBaseContext(), android.R.layout.simple_list_item_1, new ArrayList<String>());
idList.setAdapter(str);
for(String userIds : myStrings) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.getInBackground(userIds, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject parseObject, ParseException e) {
String name = parseObject.getString("fullname");
Log.d("These are the names: ", name);
str.add(name);
users.add(parseObject);
}
});
}
idList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ParseObject user = users.get(position);
Toast.makeText(UserListForHistory.this, user.getString("userIDs"), Toast.LENGTH_SHORT).show();
}
});
如果不起作用,请在下面留言。