【问题标题】:Android: trying to show info based on what user profile hasAndroid:尝试根据用户个人资料显示信息
【发布时间】:2014-11-18 10:09:32
【问题描述】:

我有一个ListView,每一行都包含一个不同的User 对象。

User 具有以下不同的字段:

  • 我喜欢的东西列表
  • 我讨厌的事情清单
  • 我玩的游戏列表
  • 自我描述

现在,当我填充ListView 时,我想显示每个用户填写的不同内容。例如,一行可能会显示填写的“我喜欢的东西”,而另一行可能会显示“我自己的描述”。

我不希望相邻行显示相同类型的行。所有这些代码都将插入我的BaseAdapter's getView methood

pseudocode 而言,实现这一目标的最佳逻辑是什么?

提前致谢!!

更新:解决方案

// 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
    }
}

【问题讨论】:

    标签: android listview logic


    【解决方案1】:

    您可以尝试这种方法,在您的 BaseAdapter 中验证您要显示的列表。

    例如:

    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
    }
    

    代码:

    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;
        }
    
    }
    

    你也可以做 random.nextInt(4) 但它可能每行都有相同的结果。

    【讨论】:

    • 感谢您的回答!我没有像你的代码那样做,但它给了我如何去做的想法。在我的 OP 中发布答案。
    • 我很高兴它给了你想法。 :)
    【解决方案2】:

    我认为您尝试做的事情可以使用 ArrayAdapter 和自定义行布局来实现。在this article 中有很好的解释。我建议您阅读全文,但第 6 节解释了您想要做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多