【问题标题】:Opening different classes by Intent according to the position from the OnItemClickListener根据来自 OnItemClickListener 的位置通过 Intent 打开不同的类
【发布时间】:2011-05-31 13:10:12
【问题描述】:

我想在单击 ListView 时打开不同的类。我已经创建了 onItemClickListener 并且我正在打开类,但我只能打开一个类,而不是能够打开与列表中的项目关联的类。

OnItemClickLcisterer里面有个参数叫“int position”但是不知道怎么用。

我的列表中有很多不同的项目,我想从与列表中的项目关联的 OnItemClickListener 中打开一个特定的类。

在这里,我将我正在做的事情放在我的 OnItemClickListener 中:

nameOfStates.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { Intent mIntent = new Intent(StateSelection.this, AlaskaZone.class); startActivity(mIntent);

                Intent myIntent = new Intent(StateSelection.this, AlabamaZone.class);
                startActivity(myIntent); 

            }
        });

我的列表中有 Item(name) Alaska 应该打开 AlaskaZone 类,同样,myh 列表中的 Item(name) Alabama 应该打开 AlabamaZone 类。

我该怎么做呢?

谁能帮帮我?

谢谢, 大卫

【问题讨论】:

    标签: android class listview click


    【解决方案1】:

    在你的听众中很简单,放置 switch-case 块并使用位置,对于情况,0 位置反映列表中的第一个项目,依此类推,并且对于每种情况开始不同的活动

    nameOfStates.setOnItemClickListener(new OnItemClickListener() 
        { 
            public void onItemClick(AdapterView parent, View view, int position, long id) 
            { 
                switch (position)
                {
                    case 0:
                        // Start activity that you want to when 1st item of your list is selected
                        break;
    
                    case 1:
                        // Start activity that you want to when 2nd item of your list is selected
                        break;
                }               
            }
        });
    

    【讨论】:

    • 你能告诉我吗,因为我已经尝试过但做不到。可能是我在编码中犯了一些错误。
    【解决方案2】:

    使用 onItemClick 方法中的参数postion 来识别已单击的行,这反过来将帮助您识别状态

    【讨论】:

    • @Frieze- 请告诉我如何使用该职位,因为我尝试了多种方法但均未成功。
    • 大卫,你如何显示列表中的状态。我的意思是您必须使用适配器来填充列表,对吗?您能否分享您的适配器代码,以便我可以指出您在哪里进行更改
    猜你喜欢
    • 1970-01-01
    • 2019-02-17
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多