TwoStarII TwoStarII - 24 days ago 11
Java Question

ListView Item Open New Activity

I'm still following a couple of tutorials in order to learn Android Development, I am struggling to open new activities. I can open only one activity, in fact all items on my

ListView
open this one activity, which would be great for
ListView
item in position 0 (the first item) not all of them.

My code below:

// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(mContext, MyActivity1.class);
mContext.startActivity(intent);
}
});

return view;
}


As you can see MyActivity1.class opens fine, but what if I had a second class called MyActivity2 which should be opened up by listview item in position 1 (second item), how would I do that? Also what does (View arg0) mean? I can't find proper explanation on this?

Answer
lv.setOnItemClickListener(n## Heading ##ew OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // When clicked, show a toast with the TextView text
        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(view.getContext(), Html_file1.class);
            startActivityForResult(myIntent, 0);
        }

        if(position == 2) {
            //code specific to 2nd list item    
            Intent myIntent = new Intent(view.getContext(), Html_file2.class);
            startActivityForResult(myIntent, 0);
        }
    }
});