Listview onItemClickListener obtaining data

113 Views Asked by At

I have a String array saved in the internal storage and I'm using it to retrieve the Names of specific users from Parse.com. This is the code:

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();

        }
    });

Is there a way to configure it so that when I click on an item that it displays the userIDs which are saved in "myString" array? Because I want to display a list with the specific names but on click I want to get that specific users objectid. The only way I know how it can be done is to just save the String UserIds directly to the adapter but then It would display the ids on the list instead of the names.

//EDIT SOLUTION

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();

        }
    });
1

There are 1 best solutions below

0
On BEST ANSWER

If i understood, this is one way of do this:

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();

    }
});

if didn't works, leave a comment below.