开发者

Android SDK - ListView, launch different intents based on which row was clicked

开发者 https://www.devze.com 2023-03-25 14:02 出处:网络
new to android development and im working on getting my iOS app to Android. In my app im using listviews, and that works great. This is what i have so far, and its inside a tabwidget.

new to android development and im working on getting my iOS app to Android. In my app im using listviews, and that works great. This is what i have so far, and its inside a tabwidget.

    setListAdapter(new ArrayAdapter<String> (this, R.layout.row_style, ITEMS));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

static final String[] ITEMS = new String[] {
    "Apple", "Google", "Hewlett Packard", "Adobe"
};

note that the ITEMS string array just is dummy data. Now here is my problem, if the user click on Apple i want to start a activity called AppleActivity. How can i with a onItemClickListener do that. In iphone sdk i can call

if 开发者_Go百科(objectAtIndex == 0)

Can i do something simular? Hope you guys can help.

Jonas :) note that the ITEMS string array just is dummy data. Now here is my problem, if the user click on Apple i want to start a activity called AppleActivity. How can i with a onItemClickListener do that. In iphone sdk i can call

if (objectAtIndex == 0)

Can i do something simular? Hope you guys can help.

Jonas :)


yes , you can , you will have something like this ->you set adapter after

    yourList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
   //arg2 represents the position , the eletement at arg 2 that is clicked 
   if (arg2==1) startActivity (new Intent ("blabla"));
   if(arg2==2) startActivity (new Intente("Adobeeee"));
   }


This is because You're trying to start Adapter, while an Activity class must be a second parameter for Intent constructor. Also this activity must be described in manifest file.


lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            if (position == 0) {
                Intent algebra = new Intent(v.getContext(), AlgebraListViewController.class);
                startActivity(algebra);
            }
            else if (position == 1) {
                Intent oekonomi = new Intent(v.getContext(), OekonomiListViewController.class);
                startActivity(oekonomi);
            }
        }
    });

This should work fine, renamed the variables so you know what they are (instead of arg0, arg1, arg2, and arg3).

0

精彩评论

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

关注公众号