Interpreting item click in ListView
        Posted  
        
            by Matt Huggins
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Matt Huggins
        
        
        
        Published on 2009-12-12T00:26:08Z
        Indexed on 
            2010/05/02
            13:08 UTC
        
        
        Read the original article
        Hit count: 238
        
I'm working on my first Android project, and I created a menu via the XML method. My activity is pretty basic, in that it loads the main layout (containing a ListView with my String array of options). Here's the code inside my Activity:
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	// allow stuff to happen when a list item is clicked
	ListView ls = (ListView)findViewById(R.id.menu);
	ls.setOnItemClickListener(new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			// @todo
		}
	});
}
Within the onItemClick callback handler, I'd like to check which item was clicked, and load a new menu based upon that.
Question 1: How do I go about figuring out which item was clicked?  I can't find any straightforward examples on retrieving/testing the id value of the clicked item.
Question 2: Once I have the id value, I assume I can just call the setContentView method again to change to another layout containing my new menu.  Is that correct?
© Stack Overflow or respective owner