Basic Listview Demo: Android Programming

Whole ListView registers as a click if the whole ListView registers as a button whereas if you work with an item quick listener it’s if you click on a specific item and that’s in fact what we want so we’re going to here I need a listener so I’m going to create a new and it happens to be an adapter view dot and on item click listener.

Now it’s not gonna be happy yet why because I need to import and then I need to actually implement the methods so at the moment we have an implement of them select Eclipse to auto fill in and so now we get this now unfortunately Eclipse has given us r1 r2 and r3 which isn’t particularly meaningful long and rename them so the first two arguing disappearance and object we’re working.

With the next one is the view clicked and then finally we have the position and the ID of what was clicked so let’s get the the list view that we’re working with so.

I’m going to come up with the it’s a text to you rather that was clicked so text view equals and we’re just gonna have to cast the view clicked because we’re actually passed in the appropriate reference then what I’m gonna do is.

Build a string I want to use so let’s call it message oh there you clicked number and I can actually show the number that’s the position which is string and then I can put on the string and so that’s going to be at my text view and on the text view I can get the text I’ll call to string all the text it.

Returns because of course it’s actually returning a character sequence oh.

And I need to concatenate that now I can just put up the message so let’s put up a toast dot make.

Text give it some context I can’t just use this actually this as a context is going to refer to my anonymous class I need to refer to the activity so I can.

Say main activity dot this so main activity got this.

What texture I want to display well I happen to have a message here what do I want it how long display for so toast dot and let’s do it long so you got more texture to read and then finally I’m going to show it run that now when I click on green get the pop up you click number one.

Just green might rise blue number zero is blue that sounds a very simple list to death or a list you we create the.

List view we specify as a layout what we want each of those items to be in this case just a simple text view I could have made it for example an image view or something more complex and we’ll look at that in a few the future tutorial in Java I do most.

Of the work and so what do I do well I first populate the list creating the elements I want to populate it with my array adapter does the conversion between the items in my array and a view which is actually gonna end up on the screen and then I tell my list I want it to set the adapter to the thing I just created so the adapters effectively my.

Constructor Maya the entity that’s going to build these list views for me or these text views for me.

As needed and then finally down here I wanted to set up the register for call black on click so I call set on item click listener and then I can do anything I like in here and specifically I’m extracting the text using the view get.