开发者

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

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.simpl开发者_JS百科e_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

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.


MyList()[position] should be myList[position].
Next you can not start an activity like this startActivity(Bradford.java);. For starting a new activity you need to create and intent and then set the Activity class. Then you can call startActivity with that intent.

Intent intent = new Intent();
intent.setClass(this, Bradford.class);
startActivity(intent);
And you need to add Bradford activity to your manifest as well.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜