简体   繁体   English

从列表视图开始活动

[英]Start an activity from a listview

Hi I have a listview and, I am trying to start an activity from the listview by startActivity(class.java); 嗨,我有一个列表视图,并且我正在尝试通过startActivity(class.java);从列表视图开始一个活动startActivity(class.java);

public class ll2 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] myList = new String[] {"Accrington Stanley", "Aldershot Town", "Barnet", "Bradford City", "Burton Albion", "Bury", "Cheltenham Town", "Chesterfield", "Crewe A", "Gillingham", "Hereford Utd", "Lincoln City", "Macclesfield T", "Morecombe", "Northampton T", "Oxford Utd", "Port Vale", "Rotherham Utd", "Shrewsbury T", "Southend Utd", "Stevenage", "Stockport C", "Torquay Utd", "Wycombe W"};              
        ListView lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
        setContentView(lv); }

        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

        if ("Bradford City".equals(MyList()[position])){
            startActivity(Bradford.java);}
        }
}

I am getting a error with this line if("Bradford City".equals(MyList()[position])) method myList() is undefined for the type ll2 我在此行遇到错误if("Bradford City".equals(MyList()[position]))方法myList()对于类型myList()未定义

I have tried all sorts of methods, and I cannot get anything to work. 我尝试了各种各样的方法,但是我什么也做不了。 All I want to do is for each team in the list have a separate java file(Class) With activities in them. 我要做的就是为列表中的每个团队创建一个单独的Java文件(类),其中包含活动。

MyList()[position] should be myList[position] . myList[position] MyList()[position]应该是myList[position]
Next you can not start an activity like this startActivity(Bradford.java); 接下来,您将无法启动像这样的活动startActivity(Bradford.java); . For starting a new activity you need to create and intent and then set the Activity class. 为了启动新活动,您需要创建并意图然后设置Activity类。 Then you can call startActivity with that intent. 然后,您可以按此意图调用startActivity。

Intent intent = new Intent();
intent.setClass(this, Bradford.class);
startActivity(intent);
And you need to add Bradford activity to your manifest as well. 并且您还需要将Bradford活动添加到清单中。

I struggled with this all day and thought id post it everywhere i could. 我整日都在为此苦苦挣扎,以为我会尽可能地把它贴出来。

I got the a little easier answer. 我得到一个简单的答案。 Basically i turned it into a string so i could just type out what i wanted where. 基本上,我把它变成了一个字符串,所以我可以只输入我想要的位置。

protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    if (item.equals("Economy"))
        {

    Intent intent = new Intent(packages.this, economy.class);
        startActivity(intent);
}
    else if (item.equals("Basic"))
    {

Intent intent = new Intent(packages.this, basic.class);
    startActivity(intent);
}
    else if (item.equals("Professional"))
    {

Intent intent = new Intent(packages.this, professional.class);
    startActivity(intent);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM