开发者

Android: Using the same ListView for different lists

开发者 https://www.devze.com 2023-01-19 12:34 出处:网络
I have a 3 buttons, say LanguageButton, CountryButton and PinCodeButton. When LanguageButton is pressed, I must display a list of languages. Similarly for CountryButton a list of countries and so on.

I have a 3 buttons, say LanguageButton, CountryButton and PinCodeButton. When LanguageButton is pressed, I must display a list of languages. Similarly for CountryButton a list of countries and so on. Only one list is to be displayed at a time.

My question is whether it is better to define a single ListView in my layout or 3 separate ListVi开发者_如何学JAVAews for each list in my layout!

I tried to use a single ListView and set corresponding adapters when different buttons are pressed, i.e in onClick() of languageButton, I give setAdapter(languageArray); But my doubt arose in implementing the onItemClick() of the list. Because what must be done whenfirst item is clicked in languageList is different from what must be done in countryList. Hence that would add to more code in the java file.

So I am just wondering whether I should simplify my onItemClick() code by defining separate ListViews, or should I simplify my layout and add logic to code?

Regards, Kiki


Personally, I would probably go with just one ListView for all three. You can just change the content of it dynamically depending on the action event.


I would use one ListView. Depending on which ListView you are binding, just use an IF/ELSE IF or SWITCH branches to do different actions in the OnItemClick event.


I would define three different ListViews and either show/hide them as appropriate or add/remove them. I'd prefer this over including three ListViews definitions within the same Activity due to readability purposes, and I would go so far as to create three different ListView classes, i.e. LanguageListView, CountryListView, and PinCodeListView that each define their own onItemClick method through setOnItemClickListener. If memory is a concern down the road you can take a look at rolling the ListViews all into one Activity, but for now I'd go the route of easier upfront maintainability and good readability.

0

精彩评论

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