【问题标题】:How to press ArrayList and display an AlertDialog containing information from another ArrayList?如何按下 ArrayList 并显示包含来自另一个 ArrayList 的信息的 AlertDialog?
【发布时间】:2012-08-11 02:50:46
【问题描述】:

好的,这就是我目前所得到的:

public class Perks extends ListActivity {

 ArrayList<Item> items = new ArrayList<Item>();
 ArrayList<Item> entry = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        items.add(new SectionItem("Tier 1"));
        items.add(new EntryItem("Flak Jacket"));
        items.add(new EntryItem("Ghost"));
        items.add(new EntryItem("Blind Eye"));
        items.add(new EntryItem("Hardline"));
        items.add(new EntryItem("Lightweight"));

        items.add(new SectionItem("Tier 2"));
        items.add(new EntryItem("Hard Wired"));
        items.add(new EntryItem("Scavenger"));
        items.add(new EntryItem("Cold Blooded"));
        items.add(new EntryItem("Toughness"));
        items.add(new EntryItem("Fast Hands"));

        items.add(new SectionItem("Tier 3"));
        items.add(new EntryItem("Engineer"));
        items.add(new EntryItem("Dead Silence"));
        items.add(new EntryItem("Extreme Conditioning"));
        items.add(new EntryItem("Tactical Mask"));
        items.add(new EntryItem("Awareness"));
        items.add(new EntryItem("Dexterity"));

        EntryAdapter adapter = new EntryAdapter(this, items);

        entry.add(new SectionItem("Tier 1"));
        entry.add(new EntryItem("Take less explosive damage."));
        entry.add(new EntryItem("Cannot be detected by enemy UAV while moving."));
        entry.add(new EntryItem("Unaffected by AI-controlled perks."));
        entry.add(new EntryItem("Receive bonus score points."));
        entry.add(new EntryItem("Move faster, take no damage from falling."));

        entry.add(new SectionItem("Tier 2"));
        entry.add(new EntryItem("Immune to counter-UAV and enemy EMPs."));
        entry.add(new EntryItem("Replenish ammo and grenades from fallen enemies."));
        entry.add(new EntryItem("Resistence to targeting systems including Dual Band, Target Finder," +
                "Sensor Grenades and player-controlled aircraft."));
        entry.add(new EntryItem("Flinch less when shot."));
        entry.add(new EntryItem("Swap grenades faster, use grenades and equipment faster, and safely throw" +
                "back frag grenades."));

        entry.add(new SectionItem("Tier 3"));
        entry.add(new EntryItem("Show enemy equipment, delay explosives, and re-roll and booby trap care" +
                "packages."));
        entry.add(new EntryItem("Move silently."));
        entry.add(new EntryItem("Sprint for a longer duration."));
        entry.add(new EntryItem("Reduce the effect of flash, concussiona and shock charges."));
        entry.add(new EntryItem("Enemy movements are easier to hear."));
        entry.add(new EntryItem("Climb ladders and mantle over objects faster, recover from melee faster and" +
                "aim faster after sprinting."));


        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        if(!items.get(position).isSection()){

            EntryItem item = (EntryItem)items.get(position);

            //Toast.makeText(this, "You clicked " + item.title , Toast.LENGTH_SHORT).show();

                  AlertDialog.Builder adb=new AlertDialog.Builder(Perks.this);
                  adb.setTitle(item.title);
                  adb.setMessage();
                  adb.setPositiveButton("Ok", null);
                  adb.show();
              }


        super.onListItemClick(l, v, position, id);
    }
}

我想要做的是拥有它,以便在按下“项目”时,它会在 AlertDialog 中显示相应的“条目”。任何有关此事的帮助将不胜感激。

谢谢:)

【问题讨论】:

    标签: android oop arraylist android-alertdialog


    【解决方案1】:

    好吧,看起来您当前的数组列表对于相应的索引具有相同的值......换句话说......如果用户在项目列表中选择项目 2,您希望在条目列表中显示项目 2。

     @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
    
            if(!items.get(position).isSection()){
    
                EntryItem item = (EntryItem)items.get(position);
                EntryItem discriptor = (EntryItem) entry.get(position);
    
                      AlertDialog.Builder adb=new AlertDialog.Builder(Perks.this);
                      adb.setTitle(item.title);
                      adb.setMessage(discriptor.getTitle()); //get the value of this element.
                      adb.setPositiveButton("Ok", null);
                      adb.show();
                  }
    
    
            super.onListItemClick(l, v, position, id);
        }
    

    否则,我个人认为只创建一个类来处理两组数据会更容易......例如

    public class EntryItem {
    
         private String name;
         private String description;
    
         public EntryItem(String name, String discription){
              this.name = name;
              this.description = discription;
         }
    }
    

    我只是认为这样管理会更容易。祝你好运!

    【讨论】:

    • 非常感谢你,你是我的英雄!我真的试图解决这个问题 6 小时 :P 哦,而不是使用 discriptor.getTitle()) 我只是使用 discriptor.title()) 并且效果很好。再次非常感谢! :)
    • 是的,对不起,我不知道你是怎么得到这个头衔的……很高兴它起作用了!!
    【解决方案2】:

    您可能会将通过EntryItem 构造函数发送的值存储到该特定类中的某个变量中。对吧?

    如果你这样做,你可以使用:

    adb.setMessage(""+item.getTheValueFromTheVariable());
    

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多