开发者

ListView and TextView subclass

I'm an Android n00b and I'm learning how to code for the Android. I'v开发者_JS百科e created a subclass of TextView and I'd like to use it to create the cells for a ListView. How can I do that?

Thanks!


You don't need to subclass it. You just need to define them in a XML. I think it's easier for you that are starting.

For example, take a look at this guide. It works "out of the box". Just copy and paste the code there and study.

My very first custom list was based on it. It provides many TextViews etc. Later you may try ExpandableListViews... I saw this some days ago. Finally, when you practice a lot and get to learn it better, I would suggest that you try custom adapters to merge different levels/cells. Those are known as "headered lists". Very powerful, but to me kind of complex.

A few days ago, I posted a question here on SO about headered lists. I talked about how Jeff Sharkey (a Google employee, IIRC) managed to do it. He made a very nice class. The people from CommonsWare also created a class for this, with the benefit of it being licensed under ASL 2 ("business-friendly"). I talked about all that in my question. There is also a link to the Google groups where I solved my problem. But since you're just starting, I would leave headered lists for later, of course.

Try reading the beginner guides I posted (the links for regular adapters), and also search Google for "custom listview". Really, it has a lot more than we can offer here, and is readily available.

Good luck!


You can refer your subclass in layout XML, you just need to make sure to use its full name. If your subclass is com.example.CustomTextView you can refer to it in XML like this:

<com.example.CustomTextView/>

If you want to use that layout in a ListView create a custom adapter (subclass BaseAdapter, ArrayAdapter, CursorAdapter, or any other adapter) that uses it, just be sure to override the method that gets the view.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜