开发者

Starting Activities from List in Android

I am writing an app and I have run into a problem. The first activity has a list with many开发者_Go百科 options, each list object consists of a picture and some text. The goal is a user picks a list option and it opens a new activity. I have a Array List with instances of a class that contains the image, text, and name of activity that needs to start. When I call StartActivity in my onListItemClick(), how can I get the activity name in the new Intent and make it accept the name? Is this possible or is this a terrible way of going at what I want? I am very new to android and am sorry for any confusion. Any help is very appreciated.


Why don't you make your custom list class that contains the image, text, and name of the activity also have another field that is an instance of a Class object of the Activity you want to start? That way, you can just use that to start the new Activity.


Another solution which might be easier than creating a custom class...create a Hashmap which contains the ListView Object and the Activity you want to start.

public static final HashMap<Object, Class> map = new HashMap<Object, Class>();
    static {
        map.put(key0, Activity0.class);
        map.put(key1, Activity1.class);
        map.put(key2, Activity2.class);
    }


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

        startActivity(new Intent(this, map.get(lv.getItemAtPosition(position));

    }

Obviously the the Object in the HashMap definition will need to be changed to the Object which you're keying off of.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜