aspose file tools*
The moose likes Android and the fly likes How to get View from customised ListView? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to get View from customised ListView?" Watch "How to get View from customised ListView?" New topic
Author

How to get View from customised ListView?

Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52

Hi all,

I have a Customised ListView which contains ImageView and TextView.

I want to get instance of the TextView in onCreate().

I tried, but m getting NullPointerException….

Can anybody please help me out…..


Here’s the code m working with…






delete_list.xml





custom_list.xml





And Here’s the Java File




please help me out.....
pratap solapur
Greenhorn

Joined: Sep 08, 2008
Posts: 2
You should do it like this




pratap solapur
Greenhorn

Joined: Sep 08, 2008
Posts: 2
You should do it like this.............

Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Thanks pratap....

i tried it but stiil m getting NullPointerException at



Bharath Jagdish
Greenhorn

Joined: Mar 13, 2009
Posts: 5
Where have you declared and initialized "mList" variable?
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Sorry Bharath,

No w i corrected it,
it's mBuddyList. Declared as data member of my class DeleteActivity.
and is initialised in onCrete()

Thanks for your reply...
Bharath Jagdish
Greenhorn

Joined: Mar 13, 2009
Posts: 5
I doubt whether you can access the list items as soon as you set the adapter. Correct me if i am wrong.

Try doing operations by adding any listener to the listview.

Try checking the childcount of the list view after you set the adapter. It would be 0.
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Yes, we can access th child items. i achieved it by OnItemClickListener.

Here's the sample code.


Bharath Jagdish
Greenhorn

Joined: Mar 13, 2009
Posts: 5
So that solves the problem? Accessing the items in listeners rather than in onCreate(). Or you want that to be accessed in onCreate() only?

What i feel is, till the execution of onCreate() completes, the views wont be bound. so you will not be able to access the TextView within onCreate(). If you still want to access those variables outside the listeners, then try using Handler and post in in the queue for the thread so that onCreate() method execution is completed.

handlerInstance.post(runnableInstance);


Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakta,

I am facing a problem in list view, i want to add two buttons, say add and delete in list row and the same time i want to listen both button's click event with particularly list's on click event.

Here is the list design that would be look like,
+------------------------------------------+
| textview.............button1 button2 |
+------------------------------------------+
| textview.............button1 button2 |
+------------------------------------------+
I want to get click event of 1st row's button1's click event. Is it possible? In short i want to get view type on list's click event.

Thanks & Regards
Pratik Goswami


iOS * Android Developer
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Hi Pratik,
first of all, it's not Prajakta, it's Prajakti....

Anyways,

I tried what you wanted to achieve but actually you can't handle Button Click events from list (I don't know the reason)

So for that i've used TextView instead of Button and the handled the events...

m attaching the files herewith for your reference....

Java File





main.xml





and test.xml




Hope it'd help you....
Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakta, (Sorry Prajakti :wink

Thank you very much for quick reply.

While running your given sample code i found that while clicking any button first time on a list row Toast doesn't comes up to screen. I have to click on list row fisrt and then only I am able to can get button's click event. I have to follow this procedure for all list rows. Are you getting the same problem?

Thanks & Regards
Pratik Goswami
Prajkti Khadse
Ranch Hand

Joined: Apr 02, 2009
Posts: 52
Yes....
Because on the first click, we get View (v.findViewById())
and on the second click only we get the OnClick() event...... As far as i know, we've no option but to double click.........
Pratik Goswami
Ranch Hand

Joined: Mar 02, 2009
Posts: 136

Hi Prajakti,

Thanks for reply,

I found a thread in between to focus multiple actions/events on list. Here it is.

Thanks & Regards
Pratik Goswami
Joemarie Amparo
Greenhorn

Joined: Feb 05, 2011
Posts: 1
Hello,

do you have sample codes / tutorials wherein a listview will display an Image and a Text at the side of the image.


Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get View from customised ListView?