Home > DeveloperSection > Forums > Hot to show info based on what user profile has in android?
Andrew Watson
Andrew Watson

Total Post:39

Points:273
Posted on    November-18-2014 10:24 PM

 Android ListView 
Ratings:


 1 Reply(s)
 484  View(s)
Rate this:

I have a ListView and each row contains a different User object.

A User has these different fields:

  • List of things I like
  • List of things I hate
  • List of games I play
  • Description of myself

Now, when I populate the ListView, I want to show different things that each user has filled out. So for example, one row might show 'things I like' filled out, while another might show the 'description of myself'.

I don't want to adjacent rows to be showing the same type of row. All this code will be inserted into my BaseAdapter's getView methood.

What would be the best logic in terms of pseudocode to achieve this?

// Creates arraylist of elements that are complete in profile

List<String> profileElements = new ArrayList<String>();

if (user.getGameOwned() != null && user.getGameOwned().size() > 2) {

    profileElements.add(KEY_GAMES_PLAYED);

}

if (user.getAboutMe() != null && !user.getAboutMe().isEmpty()) {

    profileElements.add(KEY_ABOUT_ME);

}

if (user.getIAm() != null && user.getIAm().length > 2) {

    profileElements.add(KEY_I_AM);

}

if (profileElements.size() > 0) {

    String randomElement = profileElements.get(new Random().nextInt(profileElements.size() - 1));

    if (randomElement.equals(KEY_GAMES_PLAYED)) {

        // do whatever here

    }

    else if (randomElement.equals(KEY_ABOUT_ME)) {

        // do whatever

    }

}



Chris S
Chris S

Total Post:48

Points:336
Posted on    November-19-2014 12:04 AM


You could try this approach, in your BaseAdapter put a verification if what list do you want to show.


For example:

int ctr = 1; // your counter - try to declare it on the global level

if(ctr<4){//your 4 types of list -

     display(ctr); //print the current ctr

     ctr++;

}else{

     display(ctr);

     ctr = 1; //make the value to 1 for the next batch of loop

}

code:

display(int index){

 

    switch (index){

        case 1:

             //List of things I like

            break;

        case 2:

             //List of things I hate

            break;

        case 3:

            //List of games I play

            break;

        case 4:

             //Description of myself

            break;

        default:

            break;

    }

}

You could also do random.nextInt(4) but it might have the same result each row.


Modified On Mar-14-2016 06:54:55 AM

Don't want to miss updates? Please click the below button!

Follow MindStick