Android distribute data in a ListView -


I'm having difficulties spreading data on ListView

As the list matches: list (string category, string name, string description, string address) and I have

  list.add ("Drink", "Water", " Fresh Water ", false) is a list. Add ("Food", "Burger", "Homemade", False) ("Drink", "Cola", "8oZ", etc.) ...   

etc. .add now ...

Now, when I call the adapter, the GetView method is called. I implemented it in this form:

  Public Class EntryRestaurantAdapter ArrayAdapter and lt; RestaurantMenu & gt; {Private context reference; Private Arrestist & lt; Restaurant Menu & gt; item; Private layoutInflator Wi; Private static final integer TYPE_SECTION = 1; Private static ending end TYPE_ITEM = 2; Public listing restoria adapter (reference reference, ArrayList & lt; RestaurantMenu & gt; items) {super (references, 0, items); This.context = context; this. Item = item; Vi = (Layout Inflator) context.getSystemService (References LAOUT_INFLATER_SERVICE); } @ Override Public Entry Typecount () {Return 2; } @ Override public entity item type (int position) {return item.get (position) .getSection ()? TYPE_SECTION: TYPE_ITEM; } @ Override Public View getView (int position, seeviewview, ViewGroup parent) {see v = convertView; Last Restaurant Menu i = items.get (status); If (! I.getSection ()) {RestaurantMenu si = (restaurant menu) i; V = vi.inflate (R.layout.list_item_section_section, null); V.setOnClickListener (zero); V.setOnLongClickListener (zero); V.setLongClickable (False); See final text view section = (text view) v.findViewById (R.id.list_item_section_text); SectionView.setText (si.getCategory ()); (Int a = 0; a & lt; items.size (); a ++) {// if (items.indexOf (i.getCategory ()) == item.lastIndexOf (i.getCategory ())) // v = vi .inflate (R.layout.item_view, null); If (i.getcategory (.) Equals (items.get (a) .getCategory ())) {log d. ("Dentro delfor", "" + Item. Gate (A) .getSection ()); Items.get (a) .setSection (true); Log D. ("Dentro del Foor del truth", "+ Items. Gate (A) .getSection ());}}} {V = vi.inflate (R.layout.item_view, null); TextView sectionView = (TextView) v. FindViewById (R.id.restaurant_name); Restaurant menu C = (Restaurant menu) I; sectionView.setText (si.getFoodName ());} Log D. ("Access section:", "" + i.getSection () + "ID:" + i.getcategory ()); Return V;}   

I called my main activity in this form:

  Entryrestorion adapter adapter = New entry resort adapter (this, restaurant menlist); listview.setAdapter (adapter);   

This works, but it leaves the first one of each category. I think this is because the getView method runs once for each condition. That it gives V as a category and not on the first try of each class as an item. How can I decide it?

This is output

Category: drink item cola (it was left excluding water item) (Food items of this burger are omitted)

You can add these two methods to your adapter Override:

  public int getViewTypeCount () {...} public int getItemViewType {int} {...}   

In the first place Should go back to how many types of scenes you have in the list, and in the second you should return the scene type to the position, which can be an integer, something like that.

  Private constant final integer TYPE_SECTION = 1; Private static ending end TYPE_ITEM = 2; .... @ Override Public Entry Tippacount {Return 2; } @ Override public entity item type (int position) {return item.get (position) .getSection ()? TYPE_SECTION: TYPE_ITEM; }   

By the way, you are not reusing the view found in the convertview, but always create a new one that is bad for both display and memory.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -